Re: what is the RIGHT THING to make a constructor of a subclass, using super()
Tom Anderson <twic@urchin.earth.li> writes:
public class SlashSeparatedCustomer extends Customer {
public SlashSeparatedCustomer(String name) {
// THIS IS ILLEGAL!
int slash = name.indexOf('/') ;
String first = name.substring(0, slash) ;
String last = name.substring(slash + 1) ;
super(first, last) ;
}
}
Quite complicated, and still not perfect, because it
is not safe for multithreading or recursion (due to
the use of static fields):
class Customer
{ public Customer
( final java.lang.String first, final java.lang.String last )
{} }
class SlashSeparatedCustomer extends Customer
{
private static java.lang.String first;
private static java.lang.String first( final java.lang.String name )
{ if( !initialized )initialize( name ); return first; }
private static java.lang.String last;
private static java.lang.String last( final java.lang.String name )
{ if( !initialized )initialize( name ); return last; }
private static boolean initialized;
private static void initialize( final java.lang.String name )
{ if( !initialized )
{ final int slash = name.indexOf( '/' ) ;
first = name.substring( 0, slash );
last = name.substring( slash + 1 ); }}
public SlashSeparatedCustomer( final java.lang.String name )
{ super( first( name ), last( name )); }}