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,300 points
432 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,830 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,220 points
2,499 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,120 points
2,726 views
0 votes
2 answers

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,190 points
39,560 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, 2019 in Java by Sirajul
• 59,190 points
1,922 views
0 votes
1 answer

How do I copy an object in Java?

Create a copy constructor: class DummyBean { ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,890 points
154 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 67,287 views
+1 vote
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,500 points
6,999 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,500 points
15,022 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, 2019 in Java by Sirajul
• 59,190 points
8,435 views
+1 vote
4 answers

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

There is already answer wriiten using StringBuilder ...READ MORE

answered Dec 16, 2020 in Java by Rajiv
• 8,910 points
27,002 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP