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,140 points
26 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,590 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
103 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,020 points
157 views
0 votes
1 answer

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,150 points
28 views
0 votes
1 answer

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

I think it is most useful as ...READ MORE

answered May 4, 2018 in Java by Akrati
• 3,150 points
45 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,360 points

edited Nov 6, 2018 by Omkar 4,628 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,030 points
1,234 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,030 points
3,525 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,030 points
221 views
0 votes
1 answer

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

Please check the below code to arrive ...READ MORE

answered Nov 21, 2018 in Java by Frankie
• 9,590 points
50 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,590 points
106 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.