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
263 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,220 points
2,188 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,110 points
2,037 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, 2019 in Java by Sirajul
• 59,070 points
9,126 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,070 points
1,100 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,950 points
65 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 49,550 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
5,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,500 points
13,055 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,070 points
5,472 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,890 points
23,726 views