How do I copy an object in Java

0 votes

Consider the code below:

DummyBean dum = new DummyBean();
dum.setDummy("foo");
System.out.println(dum.getDummy()); // prints 'foo'

DummyBean dumtwo = dum;
System.out.println(dumtwo.getDummy()); // prints 'foo'

dum.setDummy("bar");
System.out.println(dumtwo.getDummy()); // prints 'bar' but it should print 'foo'

So, I want to copy the dum to dumtwo and change dum without affecting the dumtwo. But the code above is not doing that. When I change something in dum, the same change is happening in dumtwo also.

I guess, when I say dumtwo = dum, Java copies the reference only. So, is there any way to create a fresh copy of dum and assign it to dumtwo?

Dec 30, 2020 in Java by Rajiv
• 8,910 points
117 views

1 answer to this question.

0 votes

Create a copy constructor:

class DummyBean {
  private String dummy;

  public DummyBean(DummyBean another) {
    this.dummy = another.dummy; // you can access  
  }
}

Every object has also a clone method which can be used to copy the object but doesn't use it. It's way too easy to create a class and do improper clone method

answered Dec 30, 2020 by Gitika
• 65,870 points

Related Questions In Java

0 votes
2 answers

How do I convert a String to an int in Java?

Use the lines of code mentioned below:- String ...READ MORE

answered Feb 9 in Java by Soham
• 8,740 points
455 views
0 votes
0 answers

How do I declare and initialize an array in Java?

How do I declare and initialize an ...READ MORE

Feb 8 in Java by Edureka
• 120 points
50 views
0 votes
0 answers

How do I declare and initialize an array in Java?

How do I declare and initialize an ...READ MORE

Apr 19 in Java by Rahul
• 3,380 points
32 views
0 votes
0 answers

How do I declare and initialize an array in Java?

How can I declare and initialize an ...READ MORE

5 days ago in Java by krishna
• 2,540 points
6 views
0 votes
2 answers

Copying Objects in Java

class DummyBean { private String dummy; ...READ MORE

answered Aug 6, 2018 in Java by Sushmita
• 6,900 points
169 views
0 votes
2 answers

Array of Objects

You can also do : A[] a = ...READ MORE

answered Aug 3, 2018 in Java by sharth
• 3,350 points
253 views
0 votes
2 answers

Is there a destructor in Java?

try (BufferedReader br = new BufferedReader(new FileReader(path))) ...READ MORE

answered Sep 4, 2018 in Java by Sushmita
• 6,900 points
483 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
485 views
0 votes
1 answer

How do I determine whether an array contains a particular value in Java?

Arrays.asList(yourArray).contains(yourValue) Warning: this doesn't work for arrays of ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,870 points
181 views
+15 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
235,584 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP