This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Query about constructors  RSS feed

 
Ranch Hand
Posts: 156
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling super() and this() at the same time within a constructor results in a compiler error
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call this() in one constructor and in that constructor again use super. This is the only way to achive your query...
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:



 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@mansukhdeep- I think you are giving suggestion to founder of this website..
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 3321
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!