Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using super and this in the constructor together 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using super and this in the constructor together" Watch "Using super and this in the constructor together" New topic
Author

Using super and this in the constructor together

Ayumu Osamu
Greenhorn

Joined: Dec 17, 2010
Posts: 4

I always thought that the first line in the constructor is either a super() or a this(). However, this code gives me unexpected output.


And outputs:


Why is it so?


SCJP6/OCPJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

The this() call in the 2nd constructor will call the 1st constructor (the no-arg one) and the first constructor will call the super constructor. As simple as that


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ayumu Osamu
Greenhorn

Joined: Dec 17, 2010
Posts: 4

Ankit Garg wrote:The this() call in the 2nd constructor will call the 1st constructor (the no-arg one) and the first constructor will call the super constructor. As simple as that


Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using super and this in the constructor together
 
Similar Threads
When the a, and b are eligible for GC?
abstract class
SCJP / strange behaviour when using Thread-Constructor