Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using super and this in the constructor together  RSS feed

 
Ayumu Osamu
Greenhorn
Posts: 4
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9608
36
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ayumu Osamu
Greenhorn
Posts: 4
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!