jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes this() and super() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this() and super()" Watch "this() and super()" New topic
Author

this() and super()

T George
Greenhorn

Joined: Jun 26, 2008
Posts: 9
Hello all,

I have been roaming around in here for quite some time and it is always very helpful. However this is my first post.
Now straight to business

I know(i think in know) that you can't put super() in a constructor if you have put the this() in the constructor but is it possible to put this() after a super() in a constructor.
In other words does this() also has to be on the first line in the constructor?

Thank you in advance
Thomas
Cm Ananth
Ranch Hand

Joined: Oct 20, 2005
Posts: 44
Yes.. this() and super() needs to be the first statement..


By Ananth, Bangalore
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Thomas,
Yes. Since the constructor creates an object, only one may be called. If you want to call both this() and super(), you can have the other constructor do so for you. A common technique is:



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

And welcome to JavaRanch!
T George
Greenhorn

Joined: Jun 26, 2008
Posts: 9
Thank you Ananth and Jeanne for the help.
Appreciate it
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: this() and super()
 
Similar Threads
flow of execution
Super ()
Super and Sub class
Constructors ??
Calling constructor from another constructor