I have a class Foo
in which reside these 2 methods:
save() and load()
void save() { ... }
static final Foo load() { ... }
The purpose of these methods are to save and load Foo objects via Java Serialization
However, I've realized now that they are fundamentally flawed for what I need to do - namely because in order to load data, I must create a brand-spanking new object of class Foo.
What I need to do instead is to call load from within Foo sometimes... to be able to deserialize straight into the existing Foo object (replacing the existing data present)
How can I do this?
All I can think of is to move all the fields that exist in Foo into an inner class
class Foo {
String field1;
int field2;
}
new way:
class Foo {
class Bar {
String field1;
int field2;
}
Bar data;
void load() { data = deserlializeBar(); }
}
are there any simpler ways?