aspose file tools*
The moose likes Beginning Java and the fly likes Constructor rule : when is this() inserted by compiler ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor rule : when is this() inserted by compiler ?" Watch "Constructor rule : when is this() inserted by compiler ?" New topic
Author

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

ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

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

Joined: Aug 16, 2005
Posts: 14350
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
ashwin bhawsar
Ranch Hand

Joined: Mar 16, 2011
Posts: 62

Thanks a lot for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor rule : when is this() inserted by compiler ?