File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling method right before calling super constructor

 
Nasser FF
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 () {
super();
//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
super();
}
}

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

Thanks 4 your attention
Nasseria
 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....*
 
Nishant Arora
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't think its possible.super must be always first line.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2851
10
Fedora Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nasser, did you really read the message? Because your name is still wrong.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic