A little while back I wrote a class that passed an instance into a routine and looped several times. What I needed was for the instance to be as it was intially with some fields set and some reverting to how they were before the field values were set.
When returning from the work class it would always have populated fields as expected. I did a rubbish hack and recreated
the instance and initialised some of the fields that needed pre-initialising.
I know this is probably a newbie question but how would i have saved the instance in the initialised state before the worker
class populated the rest of the fields. If i tried to save it to another Object it would point to the same instance and all the
changes were reflected in the cloned instance.
Is this a case of using a clone method or using the New keyword.
Sorry this is so dense and badly explained.

May 16th, 2013, 10:15 AM

KevinWorkman

Re: how to maintain a unpopulated class -newbie level

What happened when you tried cloning?

May 16th, 2013, 11:02 AM

wuwu_sister

Re: how to maintain a unpopulated class -newbie level

It did not clone the required fields - would have to have written bespoke clone method which is the same effort as the hack I did.

If i tried to save it to another Object it would point to the same instance and all the changes were reflected in the cloned instance.

Objects are passed by reference in Java. My guess is that when you "saved it to another Object" all you did was make a second variable point at the same existing Object, and then proceed to modify this object by the worker class. Show the code used to "save it to another Object" before the worker class does the modifications.