Copying Objects in Java

0 votes

Consider the code below:

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

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

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?

Apr 24, 2018 in Java by Daisy
• 8,020 points

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Deep Cloning is your answer, which requires implementing the Cloneable interface and overriding the clone() method.

public class DummyBean implements Cloneable {

   private String dummy;

   public void setDummy(String dummy) {
      this.dummy = dummy;

   public String getDummy() {
      return dummy;

   public Object clone() throws CloneNotSupportedException {
      DummyBean cloned = (DummyBean)super.clone();
      // the above is applicable in case of primitive member types, 
      // however, in case of non primitive types
      // cloned.setNonPrimitiveType(cloned.getNonPrimitiveType().clone());
      return cloned;
answered Apr 24, 2018 by Rishabh
• 3,520 points
0 votes
class DummyBean {
  private String dummy;

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

Every object also has a clone method which can be used to copy the object, but don't use it. It is way too easy to create a class and do improper clone method. If you are going to do that, read at least what Joshua Bloch has to say about it in Effective Java.

answered Aug 6, 2018 by Sushmita
• 6,820 points

Related Questions In Java

0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,280 points
0 votes
1 answer

Deep copying a 2d array in Java

I guess, for deep copying you will ...READ MORE

answered Nov 12, 2018 in Java by code.reaper12
• 3,450 points
0 votes
1 answer

How can we compare objects by multiple fields in Java?

You can implement a Comparator which compares two Person objects, and ...READ MORE

answered Jan 9 in Java by Daisy
• 8,020 points
+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,396 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,310 points
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,820 points
0 votes
1 answer

What is the 'instanceof' operator used for in Java?

It's an operator that returns true if ...READ MORE

answered May 23, 2018 in Java by Rishabh
• 3,520 points
0 votes
2 answers

Determining Class of an Object in Java

You can use: Object instance = new SomeClass(); instance.getClass().getName(); ...READ MORE

answered Nov 26, 2018 in Java by Sushmita
• 6,820 points
0 votes
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,520 points
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points

© 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.