Open In App

CompoundName clone() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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()



Last Updated : 27 Mar, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads