title: How do I copy an object in Java?
body: <p>Consider the code below:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>So, I want to copy the <code>dum</code> to <code>dumtwo</code> and change <code>dum</code> without affecting the <code>dumtwo</code>. But the code above is not doing that. When I change something in <code>dum</code>, the same change is happening in <code>dumtwo</code> also.</p>
  
  <p>I guess, when I say <code>dumtwo = dum</code>, Java copies the <strong>reference only</strong>. So, is there any way to create a fresh copy of <code>dum</code> and assign it to <code>dumtwo</code>?</p>