This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Calling constructor from another constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling constructor from another constructor" Watch "Calling constructor from another constructor" New topic
Author

Calling constructor from another constructor

nitinram agarwal
Ranch Hand

Joined: Jan 29, 2009
Posts: 83
Hi,

I would like to know, why we cannot call the same class contructor from another constructor of the same class along with calling a super class constructor. what may be the rationale behind it.

For ex.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

nitinram agarwal wrote:Hi,
I would like to know, why we cannot call the same class contructor from another constructor of the same class along with calling a super class constructor. what may be the rationale behind it.
[/code]


The reason is because the compiler won't allow you to call the constructor of the super class twice. Hence, it will either allow you to call the super() constructor (as the first line), or the this() constructor (as the first line). If you use the this() constructor, the super() constructor will actually not be called (no implicit call), because it will be assumed that the other constructor will do it.

In your case, you can't call both the super() and this() because this will attempt to construct the super class twice. The other constructor doesn't have a call to either super() or this(), and hence, has an implicit call to the super() no-arg constructor.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling constructor from another constructor
 
Similar Threads
Super class constructor
A simple Q but still have doubt
Constructor
Base Class , Super class constructors ****
keyword Super