What is object cloning in Java?

0 votes
May I know how to create an exact copy of object in Java?
Nov 29, 2018 in Java by Neha
• 6,280 points
42 views

1 answer to this question.

0 votes

Object cloning means to create an exact copy of the original object.

If a class needs to support cloning, it must implement java.lang.Cloneable interface and override clone() method from Object class. Syntax of the clone() method is :

protected Object clone() throws CloneNotSupportedException

If the object’s class doesn’t implement Cloneable interface then it throws an exception ‘CloneNotSupportedException’ .

Example:

class Test implements Cloneable

{

    int a;

    int b;

  

    // Parameterized constructor

    Test(int a, int b)

    {

        this.a = a;

        this.b = b;

    }

  

    // Method that calls clone()

    Test cloning()

    {

        try

        {

            return (Test) super.clone();

        }

        catch(CloneNotSupportedException e)

        {

            System.out.println("CloneNotSupportedException is caught");

            return this;

        }

    }

}

  

class demo

{

    public static void main(String args[])

    {

        Test obj1 = new Test(1, 2);

        Test obj2 = obj1.cloning();

        obj1.a = 3;

        obj1.b = 4;

        System.out.println("Object2 is a clone of object1");

        System.out.println("obj1.a = " + obj1.a + " obj1.b = " + obj1.b);

        System.out.println("obj2.a = " + obj2.a + " obj2.b = " + obj2.b);

    }

}

answered Nov 29, 2018 by Frankie
• 9,810 points

Related Questions In Java

+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
316 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,050 points
459 views
0 votes
2 answers

What is the difference between Set and List in java?

LIST SET Duplicates YES NO Order Ordered Depends on Implementation Positional Access YES NO List is an ordered sequence ...READ MORE

answered Aug 14 in Java by Sirajul
• 25,220 points
104 views
0 votes
2 answers

What is the use of @Override annotation in Java ? When do we use it ?

@Override annotation is used when we override ...READ MORE

answered Aug 14 in Java by Sirajul
• 25,220 points
89 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 8,029 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,963 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,112 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
492 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19 in Java by Sirajul
• 25,220 points
107 views
+1 vote
1 answer

What is a simple way to repeat a string in java?

From Java 11 on, there's a method String::repeat that does ...READ MORE

answered Aug 28, 2018 in Java by Frankie
• 9,810 points
396 views