This post originated from an RSS feed registered with Java Buzz
by Marc Logemann.
Original Post: Use constructor chaining Part II
Feed Title: Marc's Java Blog
Feed URL: http://www.logemann.org/day/index_java.xml
Feed Description: Java related topics for all major areas. So you will see J2ME, J2SE and J2EE issues here.
Since my entry on chaining constructors produced some feedback here and there, i want to go further on this topic.
First of all, i am not far away from Cedrics oppinion, in fact i am using private init() methods all over the place, but i also pointed out a risk on using them (see comments on cedrics blog). I am not totally with him on the point of readibility, but i agree on not putting too much code in a constructor.
One variant would be to use init() _and_ this-constructors. Here you call init() only in the last constructor in the chain to do the stuff you wanna do with your attributes and general state of the object.
BTW, using init() methods for construction is a pattern years in usage, so nobody would argue against it. Even the first versions of JBuilder and other IDEs used this approach when generating source code from their GUI builder.
Overall, we share one goal: defining one place where the initialisation of objects occur and trying to avoid having 10 places where the object will be initialised one step after another or even worse, 10 places with redundant init code.