B
Ben Wilson
Hi, everyone,
I've google'd all around for an answer to this, but I'm not satisfied with
what I've found. Hope someone can help.
I've got a conceptual problem of consumption. I'm making instances of
complicated objects, and whenever I need a new one, I just make a new one
with the new keyword. All the initialisation is put nice & where it belongs
in the initializers and the constructors. If I have an old object but want a
new one, it's handy that I've got everything in the initializers and
constructors. I just forget about the old one, let garbage collection take
care of it, and create my new object by passing parameters to the
constructors and maybe tweaking a few fields. This is comsumption and it's
great.
Now, what if I want to do this with the entire class? And reinitialise all
of the static data? By using all of the initializers, instead of going in
and manually resetting everything to its original state?
The only things I've been able to find are pooh-poohing "you should never
have to unload a class" and recommendations that "unloading classes should
never reset static data". But what if that is exactly what I want? If we're
talking about instances, what I certainly DON'T do would be to take an
existing instance, invoke some method that does everything my constructor
does and reset the values of fields to whatever they were originally.
From a design perspective, it feels wrong to have to re-code all of the
class-level initialization logic in some method with an arbitrary name. This
is just duplicate code. What can you do in Java to make this happen for
static?
Thanks in advance,
Ben.
I've google'd all around for an answer to this, but I'm not satisfied with
what I've found. Hope someone can help.
I've got a conceptual problem of consumption. I'm making instances of
complicated objects, and whenever I need a new one, I just make a new one
with the new keyword. All the initialisation is put nice & where it belongs
in the initializers and the constructors. If I have an old object but want a
new one, it's handy that I've got everything in the initializers and
constructors. I just forget about the old one, let garbage collection take
care of it, and create my new object by passing parameters to the
constructors and maybe tweaking a few fields. This is comsumption and it's
great.
Now, what if I want to do this with the entire class? And reinitialise all
of the static data? By using all of the initializers, instead of going in
and manually resetting everything to its original state?
The only things I've been able to find are pooh-poohing "you should never
have to unload a class" and recommendations that "unloading classes should
never reset static data". But what if that is exactly what I want? If we're
talking about instances, what I certainly DON'T do would be to take an
existing instance, invoke some method that does everything my constructor
does and reset the values of fields to whatever they were originally.
From a design perspective, it feels wrong to have to re-code all of the
class-level initialization logic in some method with an arbitrary name. This
is just duplicate code. What can you do in Java to make this happen for
static?
Thanks in advance,
Ben.