aspose file tools*
The moose likes Beginning Java and the fly likes regarding this or super call in constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regarding this or super call in constructors" Watch "regarding this or super call in constructors" New topic
Author

regarding this or super call in constructors

abin joy
Ranch Hand

Joined: Jul 29, 2008
Posts: 35
a call to this or super should be the first line of any constructor if we are providing them?Why cant we have them anywhere in our constructor code?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

if you instantiate subclass constructor will run after super class constructor. so super is used to call super class constructor so it should first line.. more over this is used to overload the constructor so it should first line of constructor.

you can use super anywhere inside a method

hope this helps
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Not quite "this" or "super", but actually "this()" or "super()".
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Since all super types of your class might have internal fields, they have to have one of their constructors run. These super constructor runs have to be completed before your first assignment within your constructor. This makes perfectly sense. Suppose you could place the super() call anywhere within your constructor and a super type had protected member variables which are to be initialized in the super() call. If you would assign values in your constructor and afterwards call super(), your values vould be overridden. Fortunately, these kinds of errors cannot happen. The first call from a constructor is always a call to a this or a super constructor. If the super type has an accessible no-arg constructor this call can be performed implicitly for you. In this case, you don't have to call super explicitly.


SCJP 5.0, SCJD in progress
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Onother example of what would go wrong if calling super() was to be permitted everywhere: It is possible to declare a field final and assign a value in the constructor. In fact, the value then has to be assigned within the constructor. Suppose such a field would be acceesible for an extending class which was allowed to put its super() call anywhere in its constructor...
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
Originally posted by abin joy:
a call to this or super should be the first line of any constructor if we are providing them?Why cant we have them anywhere in our constructor code?

yes, this() and super() both must be called from the first line of the subclass constructor. Object class is a super class of all the classes. So, if one is not defined constructor in the superclass compiler will automatically put super() call in the constructor. Because constructor chaining follows the order of topdown approach. So, one has must call super class constructor from the sub class constructor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding this or super call in constructors