Consider the following code:
[ ICODE ]
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'
[ / ICODE ]
So, I want to copy dum to dumtwo and change dum without affecting dumtwo. But the code above doesn't. When I change something in dum, the same change also occurs in dumtwo.
I guess, when I say dumtwo = dum, Java only copies the reference. So, is there a way to create a new copy of dum and assign it to dumtwo?
[ ICODE ]
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'
[ / ICODE ]
So, I want to copy dum to dumtwo and change dum without affecting dumtwo. But the code above doesn't. When I change something in dum, the same change also occurs in dumtwo.
I guess, when I say dumtwo = dum, Java only copies the reference. So, is there a way to create a new copy of dum and assign it to dumtwo?