File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure 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
  • 0
  • 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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic