CompoundName clone() method in Java with Examples

The clone() method of a javax.naming.CompoundName class is used to return a copy of this compound name object. If you made any changes to the components of this original compound name won’t affect the new copy of the CompoundName object and vice versa. The clone and this compound name share the same syntax.

Syntax:

public Object clone()

Parameters: This method accepts nothing.



Return value: This method returns non-null copy of this compound name.

Below programs illustrate the CompoundName.clone() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// CompoundName.clone()
  
import java.util.Enumeration;
import java.util.Properties;
  
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // need properties for CompoundName
        Properties props = new Properties();
        props.put("jndi.syntax.separator", "|");
        props.put("jndi.syntax.direction",
                  "left_to_right");
  
        // create CompoundName object
        CompoundName CompoundName
            = new CompoundName(
                "x|y", props);
  
        // create clone object
        CompoundName cloneCompound
            = (CompoundName)CompoundName
                  .clone();
  
        // print cloned CompoundName
        System.out.println(
            "Clone CompoundName: "
            + cloneCompound);
  
        // print members
        System.out.println("Members:");
        Enumeration<String> enumeration
            = cloneCompound.getAll();
        while (enumeration.hasMoreElements())
            System.out.print(
                enumeration.nextElement() + " ");
    }
}

chevron_right


Output:

Clone CompoundName: x|y
Members:
x y

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// CompoundName.clone() method
  
import java.util.Enumeration;
import java.util.Properties;
import javax.naming.CompoundName;
import javax.naming.InvalidNameException;
  
public class GFG {
    public static void main(String[] args)
        throws InvalidNameException
    {
  
        // need properties for CompoundName
        Properties props = new Properties();
        props.put("jndi.syntax.separator", ":");
        props.put("jndi.syntax.direction",
                  "left_to_right");
  
        // create CompoundName object
        CompoundName CompoundName
            = new CompoundName(
                "x:y:z:a:m:a:q:e",
                props);
  
        // create clone object
        CompoundName cloneCompound
            = (CompoundName)CompoundName
                  .clone();
  
        // print cloned CompoundName
        System.out.println("Clone CompoundName: "
                           + cloneCompound);
  
        // print members
        System.out.println("Members:");
        Enumeration<String> enumeration
            = cloneCompound.getAll();
        int i = 1;
        while (enumeration.hasMoreElements()) {
            System.out.println(i + ":"
                               + enumeration.nextElement());
            i++;
        }
    }
}

chevron_right


Output:

Clone CompoundName: x:y:z:a:m:a:q:e
Members:
1:x
2:y
3:z
4:a
5:m
6:a
7:q
8:e

References: https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#clone()

java-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.