Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes which one to use first between this and super? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "which one to use first between this and super?" Watch "which one to use first between this and super?" New topic

which one to use first between this and super?

Sonu Nigam

Joined: Jun 22, 2008
Posts: 2
if in a subclass we want superclass constructor to be accessed and i want to use both this and super keyword and both are written just after the constructor defined then in above case which one will be written first?
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
You can't call both a super- and this-constructor in one constructor. It's simply not allowed.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

You can only use one of the two. If you use "this", then another constructor will be called in the same class; if that constructor uses "this", then a third constructor will be called, etc. Eventually, you'll get to a constructor that doesn't use "this", and in that constructor, "super" can appear.

[Jess in Action][AskingGoodQuestions]
It is sorta covered in the JavaRanch Style Guide.
subject: which one to use first between this and super?
Similar Threads
a beginner wanna knows something about this( )
constructor calling this() and super() problem
constructor call
Query about constructors