santhosh<br />SCJP,SCWCD
Thinking is the talking of the soul with itself...-Plato
Kathy Sierra wrote:Howdy -- I think what you are trying to achieve is to first invoke an overloaded constructor and THEN come back and invoke your super constructor? That can't work because the super constructor MUST always run before any other statement in a constructor *except* for a statement that calls another constructor. In other words, you can DELAY the point at which the super constructor runs, but only by another constructor invocation.
So what you want to do is invoke an overloaded constructor and then let THAT constructor invoke super(). It would generally be a bad idea to put more code in a constructor *after* the code that invokes the overloaded constructor, although you can and it will be the last code that runs, since that constructor will be at the bottom of the call stack below all of the other overloaded and super constructors.
A typical design might be to choose ONE of your overloaded constructors as THE ONE THAT REALLY RUNS and have *that* constructor make the call to super(), and then do all of the other work that needs to be done in the constructor. The other constructors that invoke it are merely there to allow the client to invoke the constructor with less, or at least *different* types of information.
For example, suppose you have a Dog class and a constructor that takes a String for a name and a String for the breed...
Dog(String name, String breed) {
}
You might want clients to be able to construct a Dog *without* a name... so you make a constructor like:
Dog(String breed) {
}
But now imagine that you have a bunch of other set-up code. You don't want to duplicate that code in BOTH constructors, so you decide that the REAL constructor will be the one with two arguments. So you make the smaller constructor, that just takes a breed, invoke the other constructor, but supplying a default name at the same time:
Dog(String breed) {
this(getDefaultName(), breed); // this works ONLY if getDefaultName() is a static method!
}
And then you make the REAL constructor do the work and call super...
Dog(String name, String breed) {
super(name);
// now do all the other stuff
}
This way, you have only ONE place where all of your initialization code lives (and only place where you invoke the super constructor) and the *other* constructors are just there to provide more flexibility to the clients (i.e. those who are trying to make a new Dog.)
Note: when you invoke a super constructor or an overloaded constructor (using super() or this()), you MUST not include any references to instance variables or methods. The methods or variables used in an argument to an overloaded constructor or a call to a super constructor MUST be static! This is because the object being created is not yet fully-formed (the child can't completely exist until its parents have been formed!) so it is not an appropriate time to be using instance variables or calling instance methods.
Cheers,
Kathy
OCPJP
Ishan Pandya wrote:@mansukhdeep- I think you are giving suggestion to founder of this website..
Mansukhdeep Thind wrote:
Ishan Pandya wrote:@mansukhdeep- I think you are giving suggestion to founder of this website..
~ Mansukh
Jesper de Jong wrote:
Ishan Pandya wrote:@mansukhdeep- I think you are giving suggestion to founder of this website..
Maybe that's why Mansukhdeep ended his post with:
Mansukhdeep Thind wrote:
By the way, this is a post originally from 2003, when JavaRanch did not have code tags - that's a feature that was added to the forum software later than 2003.
~ Mansukh
Mansukhdeep Thind wrote:The founder is Paul Wheaton.
Tony Docherty wrote:
Mansukhdeep Thind wrote:The founder is Paul Wheaton.
I think you should read this: https://coderanch.com/how-to/java/ActiveStaff
~ Mansukh
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|