1

is there a way to update a class I am currently in with a new instance of that class? For example:

public class Test
{
  Test temp = new Test();
  this = temp;
}

I am doing this because I have a save/load feature form serializable and I need a way to update the current class from it

Java investor
  • 117
  • 3
  • 12

1 Answers1

1

No, Java variables store references to objects, so this = ... will not work. There is no way for Java to track down every place that refers to the current instance of Temp and change the memory reference that they point to.

The next-best thing may be to copy all of the fields from one object to another. That has its own set of problems, though.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315