File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can we call super() outside constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can we call super() outside constructor?" Watch "can we call super() outside constructor?" New topic

can we call super() outside constructor?

Divyya Joshi
Ranch Hand

Joined: Jul 15, 2010
Posts: 102
Hey friends !
I want know Can we make a call to super class method using keyword "super . METHOD_NAME" from outside the constructor ?
I read it somewhere we can't call super(); outside the constructor .
Kindly clarify my doubt .
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
Yes, you can:

The output is:
In class A
In class B

You need this kind of call when you override a method and you want the method to do something in addition to the superclass method. So you say: "Do the stuff defined in the superclass and then something in addition.
If you just call super(); then this is a call of the superclass constructor and that can be only done from within a subclass constructor. Even more specific it has to be the first statement in the subclass constructor.

Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

yes definitely you can call

super.<method-name> (or) super.<field-name> from the instance methods and constructor... but not in static methods...

And super() can be called only in the constructor that too as the first statement...

The fact is super() is to indicate that the initialization of inherited super class properties should be initialized before the sub-class properties are initialized so it can be done only in the constructor...

In short,

super() (or) super(args) to invoke the super class constructor in first statement of sub-class constructor...

"super." keyword used to invoke super class methods (or) access super class variables that too in instance methods only

SCJP 6 [SCJP - Old is Gold]
Matthew Brown

Joined: Apr 06, 2010
Posts: 4542

The best thing you can do, whenever you have a question "can I do ...", is to try it out. You'll remember it better that way, and it's both faster and more reliable than asking in a forum. What forums are good for is helping you understand why it works like that.
I agree. Here's the link:
subject: can we call super() outside constructor?
jQuery in Action, 3rd edition