File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Overloaded Constructors - Share the Work. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Overloaded Constructors - Share the Work." Watch "Overloaded Constructors - Share the Work." New topic
Author

Overloaded Constructors - Share the Work.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, I am going back to the basics and trying to pick up some things that I missed when I started on JAVA and this is one of them.
Let's say I have a Class that I created and I have overloaded constructors, so for an example class let's say:

As you can see, 3 of the constructors will actually share some functionallity. All 3 will do the same thing with String name.
So my question is, do I just put the funcationllity for handling String name in all 3 methods or is there a more OO approach to doing this. And likewise for handling int x in the 2nd the 3rd constructor.
I know that I could create another method called someething like handleName(String name) and call that from each constructor, but that seems the same as just adding the functionallity into each constructor. Especially if the handleString only does something really simple.
Thanks.
[ June 06, 2003: Message edited by: Gregg Bolinger ]

GenRocket - Experts at Building Test Data
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy
Just Say This()
No need to duplicate code in each constructor -- you can use the magic keyword this() to invoke your own overloaded constructors. So you make ONE that is "the real one" and the others simply invoke it using this():

The only rules you must remember are:
*If you use *this* in a constructor (to invoke an overloaded constructor), it MUST be the very first statement in the constructor.
* You cannot mix both super() and this() together in the same constructor (each requires that it be the first thing in the constructor).
* Any constructor that invokes an overloaded constructor will NOT start the superconstructor chaining process. Superconstructor chaining happens only in constructors that do not say this().
If you something that looks like this:
Foo() {
this(42);
}
Foo(int i) {
super(i);
}
* The order of invocation looks like this:
1) a constructor is invoked
2) instance variables are given default values for their type (which means they do NOT get the values they have been explicitly initialized with.
3) the constructor invokes another overloaded constructor
4) the overloaded constructor invokes super()
(at this point, instance variables STILL do not have their explicitly assigned values!)
5) super constructor chaining happens
6) all super constructors are popped off the stack and we return back to the constructor that invoked super(i)
7) instance variables are given their initial values
8) the constructor completes, and is popped off the stack
9) the first constructor invoked (the constructor that calls this()) completes.
cheers,
-Kathy
p.s. I haven't had enough coffee, so if I said anything really stupid here, I know someone will correct it
 
GeeCON Prague 2014
 
subject: Overloaded Constructors - Share the Work.