raunaq said:
hi
i wanted to know how can we call a class' constructor from within the
same class, without creating a new object.
actually i have to refresh the whole thing that happens from the
constructor.
The constructor is not a method, and you can't "call"
something that isn't a method.
If you perform a lot of initialization in the constructor,
you might instead do something like
MyClass() {
this.initialize();
}
final void initialize() {
// all your setup code here
}
Take note of the `final': You do *not* want MySubClass
to extend MyClass and override initialize(), because if it
did there'd be trouble. During the construction of a
MySubClass object -- in the phase where it's using all the
superclass' constructors to initialize their portions of
the object -- Java would call MySubClass' version of
initialize() instead of the one in MyClass. Then you'd
have a MySubClass method running before its object had been
fully initialized, and this would be a Bad Thing. `final'
prevents this; `private' would also work, or you could
use a `static' initializing method:
MyClass() {
initialize(this);
}
static void initialize(MyClass obj) {
// initialize the `obj' object
}