aspose file tools*
The moose likes Beginning Java and the fly likes Query about constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query about constructors" Watch "Query about constructors" New topic
Author

Query about constructors

santhoshkumar samala
Ranch Hand

Joined: Nov 12, 2003
Posts: 156

I know that super() or this() both should be in the first line of the constructor
but what happens in the situation if I want to call both in a constructor


santhosh<br />SCJP,SCWCD
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
Calling super() and this() at the same time within a constructor results in a compiler error


Thinking is the talking of the soul with itself...-Plato
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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
Mahesh Karehonna
Greenhorn

Joined: Apr 02, 2013
Posts: 6
Call this() in one constructor and in that constructor again use super. This is the only way to achive your query...
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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


Hey Kathy, use code tags when putting code snippets:



Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

@mansukhdeep- I think you are giving suggestion to founder of this website..


OCPJP
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Ishan Pandya wrote:@mansukhdeep- I think you are giving suggestion to founder of this website..


The founder is Paul Wheaton.

Anyways,
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


Oh, I didn't have a look at the date. This goes way back in time. That's what I was wondering. How come Kathy suddenly popped out of nowhere?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Mansukhdeep Thind wrote:The founder is Paul Wheaton.

I think you should read this: http://www.coderanch.com/how-to/java/ActiveStaff
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Tony Docherty wrote:
Mansukhdeep Thind wrote:The founder is Paul Wheaton.

I think you should read this: http://www.coderanch.com/how-to/java/ActiveStaff


Thank you Tony. RESPECT FOR KATHY & PAUL! TOTAL RESPECT!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about constructors