This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor rule : when is this() inserted by compiler ?

 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per one of the Rules of Constructors :
Every constructor has, as its first statement, either a call to an overloaded constructor ( this() ) or a call to superclass constructor( super() )

I know that compiler always insert super() as 1st statement, then what about this() when is that inserted by the compiler ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's never inserted automatically by the compiler.

this() calls the no-arguments constructor of the class from another constructor. That never happens automatically.

ashwin bhawsar wrote:I know that compiler always insert super() as 1st statement

That's not true - it only does that if you don't explicitly call super(...) (with or without arguments) yourself.
 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic