This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Calling method right before calling super constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling method right before calling super constructor" Watch "Calling method right before calling super constructor" New topic

Calling method right before calling super constructor

Nasser FF

Joined: Apr 21, 2009
Posts: 2
Calling method right before calling super constructor

Hi All,
I m new to this forum and I'll be appreciate if sb can answer to my basic question
AFAIK in inheritance section of java, it is mandatory for constructors to call its super or this in first statement of a constructor and if you dont write that code, Java copiler will automatically insert this call to its default constructor.

Fr example;

class Test () {
Test () {
//insert your code

But, the question is that if sb wana to implement any code right b4 call to super (or this) in constructor how they can do that?

class Test () {
Test () {
//before calling super method, I wana to insert code here

I want to know is there anyway to call any method right before calling super constructor?

Thanks 4 your attention
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
As far as I know you can not put code before the call to super(). The call to super is added automatically to your constructor unless you specifically put the call there. The main reason to do this is if you need to call super with some parameters or if there is no parameter-less constructor.

*third try's a charm....*

~Currently preparing for SCJP6
Nishant Arora

Joined: Apr 21, 2009
Posts: 16
i don't think its possible.super must be always first line.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
There is one way to do this: if the super constructor takes any parameters, then in the subclass you can invoke a static method that returns whatever you need to pass on to the super constructor.

Here prepare() executes before the Foo() constructor. This is only possible with a static method, because you are forbidden from accessint "this", even implicitly, before the super constructor is completed. So depending on what you're trying to do, this technique may not help you. Furthermore, I think it's probably a needlessly confusing hack that you should avoid, unless (a) you already have a super constructor with one or more arguments, that you are actually using for something, and (b) you need to call a method for the purpose of obtaining preparing the argument you need to pass to the super constructor That's why I called my method "prepare()" - I assumed it would do something specifically related to preparing (or creating or loading) the variable that gets passed to the super constructor as x. So, use this technique only if your method actually does something related to the variable you're passing to the constructor. Otherwise, you're just making your code more confusing.
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2848

"LIon KIng " welcome to Javaranch
please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.

|Asking Good Questions|
Rob Spoor

Joined: Oct 27, 2005
Posts: 20197

Nasser, did you really read the message? Because your name is still wrong.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Calling method right before calling super constructor
It's not a secret anymore!