my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors" Watch "Constructors" New topic
Author

Constructors

donal horgan
Ranch Hand

Joined: Mar 11, 2005
Posts: 54
I understand that a constructor,as it's first line(albeit explicit/implicit) must have a super() call or this() call.

A this() call is used if you have overloaded constructors before the eventual super() call.

My question is this:
If you have overloaded constructors do you HAVE to call ALL other constructors in that class using this() before a call to the super() ?

If so -- who decides the order of this() calls? The application designer?
Are there any rules for the order of the this() calls?

Or can you just make a call straight to super()?

For example -- say if you have 2 overloaded constructors which initialize int i to,say,4 and 5 respectively.
Is the value i is assigned to irrelevant?
i.e. is the relevant part that it IS initialized and not what it's initialized to.
(In which case the order of the this() calls would be irrelevant)

Please HELP !!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
This doesn't belong here, it belongs in the beginners' forum...

A constructor call should as it's first call have EITHER a call to a superconstructor OR a call to another constructor in the same class.
If you read that carefully you'll find that you can NOT have both.

If neither is provided a call to the no-argument superclass constructor (which might be implicit if none is defined in the superclass) is implied.
If a no-argument constructor does not exist in the superclass and cannot be implicitly generated for it a compilation error in the subclass results.


42
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Any or all of the constructors in a class can be called when an object is created. At least one constructor of that class, and of each of its superclasses up to Object, must be called. No constructor can call itself directly or indirectly (no recursion allowed).


Mike Gershman
SCJP 1.4, SCWCD in process
donal horgan
Ranch Hand

Joined: Mar 11, 2005
Posts: 54
As i said -- I am aware that you must have a this() or super().
Please re-read the question as to what i am asking relating to this() calls !!

P.S. It does not begin in beginners forum.

please read questions carefully before responding(particularly with such an attitude!)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
My question is this:
If you have overloaded constructors do you HAVE to call ALL other constructors in that class using this() before a call to the super() ?



no need u can straight away call the super constructor
donal horgan
Ranch Hand

Joined: Mar 11, 2005
Posts: 54
Thanks for your help Mike but what i ams asking is :
Must ALL overloaded constructors in a class be called (using this()) before you can go on and call the super() contstructor?

class A{ }
class B extends A{
B()() //constructor 1
B(int i)(); // constructor 2
}

When B gets instantiated using new B() what happens next?
(A)Does it HAVE to also execute constructor 2 in class B(using 'this')
before executing super()?
(B) Can it automatically call super() after constructor 1?
donal horgan
Ranch Hand

Joined: Mar 11, 2005
Posts: 54
Thanks Parameswaran
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272

class A{ }
class B extends A{
B()() //constructor 1
B(int i)(); // constructor 2
}

When B gets instantiated using new B() what happens next?
(A)Does it HAVE to also execute constructor 2 in class B(using 'this')
before executing super()?
(B) Can it automatically call super() after constructor 1?


B() cannot call both this and super because these calls can only be the very first statement of a constructor.

Furthermore, the call cannot be in an if statement, it must be unconditional.
Prabhaavathi Sivagnanam
Greenhorn

Joined: Mar 21, 2005
Posts: 13
Originally posted by Mike Gershman:
When an instatntiation is done, at compile time, the compiler may decide which constructor to be called and it automatically calls the super() method. So, it is not required to call all of the overloaded constructors using this(). Hope u got it.


Thanks,<br />Prabha
Jesse Torres
Ranch Hand

Joined: Mar 25, 2004
Posts: 985


Since class A doesn't have ANY constructors, the compiler will implicitly insert


So your subclass Class B, has to have a constructor that somehow calls the no arg constructor of Class A. So if you don't provide any constructors for Class B, then the compiler will implicitly insert the following to your class B:


If you type even one constructor for class B, then you have to somehow call the super class constructor. In this case, class A has a no args constructor. So somewhere one of your constructors has to have a call to super();

If class A's only constructor were A(String a, int b); Then, your class B would have to explicitly call super(String a, int b); A default constructor for class B won't suffice in that case.

To summarize things class B can have the following:

In this case the compiler will implicitly insert super(); to B() constructor.

However, if you had the following:


Then you will have to explicitly insert B() {} or a call to super(); in one of your constructors.

I hope that helps. By calling this(), you are delaying the inevitable which is a call to the super constructor.

I hope that that helps and Good luck!
[ March 21, 2005: Message edited by: Jesse Torres ]

-- <br />4 8 15 16 23 42
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Constructors
 
Similar Threads
Calling constructors of inherited classes
Overloaded Constructors - Share the Work.
constructor doubt
Query about constructors
this and super