wood burning stoves*
The moose likes Beginning Java and the fly likes Local chaining of constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Local chaining of constructors" Watch "Local chaining of constructors" New topic
Author

Local chaining of constructors

Patrick Haley
Greenhorn

Joined: Sep 15, 2004
Posts: 10
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.


Wannabe SCJP 1.4<br /> <br />It wasn't raining when Noah built the ark.<br /> --Howard Ruff
C Broussard
Greenhorn

Joined: Jun 18, 2004
Posts: 28
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
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
That's right, although defining more than one constructor isn't "overriding",
it's just defining more than one constructor, and the syntax is:


There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
 
Don't get me started about those stupid light bulbs.
 
subject: Local chaining of constructors