Skip to content
Related Articles

Related Articles

Improve Article

CompoundName clone() method in Java with Examples

  • Last Updated : 27 Mar, 2020

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:






// 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() + " ");
    }
}
Output:
Clone CompoundName: x|y
Members:
x y

Program 2:




// 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++;
        }
    }
}
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()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :