• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local chaining of constructors

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While studying for the SCJP Exam, I read about this technique, which appears to be quite nifty, but I'm wondering what practical purpose it would serve.

Has anyone locally chained constructors in applied code and, if so, to what end?

Thanks.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say locally chain constructors, are you talking about overriding constructor signatures?


like

public class someobject{
int someobject_value = 0;
public someobject(){
someobject(1);
}
public someobject(int i){
someobject_value = i;
}
}


if this is what you're talking about then yes, this is perfectly
acceptable.

hope that helped.

www.binaryfrost.com
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, although defining more than one constructor isn't "overriding",
it's just defining more than one constructor, and the syntax is:
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Has anyone locally chained constructors in applied code and, if so, to what end?



Often times you may want several constructors for convenience. By chaining them you can keep the bulk of the code in one constructor.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Patrick Haley:
Has anyone locally chained constructors in applied code and, if so, to what end?



Here's some real-world code I wrote the other day.
See if the chaining makes sense to you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic