This almost seems too obvious to be worth stating, but the principle is simple, and the error is made often...
If you have more than one constructor on a class, always forward from the least specific to the most specific - thus
public Foo( String param )
{
this( param , DEFAULT_VALUE );
}
public Foo( String param , int value )
{
this( param , value , new LinkedList( ) );
}
public Foo( String param , int value , List items )
{
this.param = param;
this.value = value;
this.items = items;
}
I quite often come across this though:
public Foo( String param )
{
this.param = param;
this.value = DEFAULT_VALUE;
}
public Foo( String param , int value )
{
this( param );
this.value = value;
}
public Foo( String param , int value , List items )
{
this( param );
this.items = items;
}
See? Absolutely chock full of subtle errors and disproportionately difficult to refactor.
If this absolutely cannot be done, then at least put static make( ) methods
onto the class, limit yourself to one constructor and make it private.
--
ShaunSmith
To have your cake and eat it too, stick to the static methods, and give them descriptive names.