calling the constructor of a class from within the class in java

R

raunaq

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.
 
P

Patricia Shanahan

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.

Building a new object is the inherent function of a Java constructor.

What are you really trying to do?

If you are just trying to save object creation by reuse, I would advise
against it, and recommend creating a new object instead.

If you need to reset some, but not all, fields, so that a new object
won't work, put the reset work in a separate method. You can call it
from the constructor, to avoid duplicate code.

Patricia
 
E

Eric Sosman

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
}
 
J

jupiter

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.

I don't follow. Constructors are for creating instances, so why
would you want to invoke one without obtaining a new instance?

Are you sure you just don't need some static members so that you
don't need to "refresh" them?
 
D

Darryl L. Pierce

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.

Why would you want to do that? The constructor's sole purpose it to set the
initial state of a new object.

If you have functionality that you want to invoke from a constructor and
also from methods in the created object, then move that code into a
_method_ and call it from both places.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top