Possible Duplicate:
How to properly override clone method?
I have a simple class that has a bunch of primitive variables in it, about 100-200 total. It's structured very simply, like this:
public class Level implements Clonable {
int speed;
boolean wallKicks;
boolean bigMode;
float targetFrequency;
long milliseconds;
double fade;
int[] perfectPenalties;
<100 or so more things like this>
}
I need to be able to clone this class. Right now, I've just explicitly typed out the copy action for each member variable in my clone() method:
public Object clone() {
Level newLevel = new Level();
newLevel.speed = speed;
newLevel.wallKicks = wallKicks;
newLevel.bigMode = bigMode;
newLevel.perfectPenalties = perfectPenalties.clone();
<etc>
}
The problem is that it's a bit of a task to be sure that I haven't missed any values in my clone method. In the future I'd also like to implement an equals() function, which would mean I'd have to manually synchronize the data in 3 places. Not pleasant.
Is there any easier way to accomplish this? I'm open to programatic solutions or anything else that does the job. I'd just like to have something easier than looking at the three side-by-side and matching variable for variable.
Thank you.