Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors

 
donal horgan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
donal horgan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Parameswaran
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesse Torres
Ranch Hand
Posts: 985
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic