Re: Smuggling information to enums
Roedy Green wrote:
I meant nested. Inner would obviously be preferable, but I thought
perhaps there might be some way of passing information to a static
nested class. Enums are certainly a peculiar kind of static nested
class. You can't do new on them. They have no constructor.
I wish you'd stop misleading people. Enums do have constructors, much
as utility classes with private constructors have them. In both
cases, client code cannot create new instances of the class willy-
nilly, but that is not the same thing as lacking a constructor.
Consider:
public final class TypeSafeEnum
{
public static final TypeSafeEnum FOO = new TypeSafeEnum( "foo" );
public static final TypeSafeEnum BAR = new TypeSafeEnum( "bar" );
private final String name;
private TypeSafeEnum( String name )
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
This class, too, has a constructor, but client code cannot do a 'new'
on it. Perfectly standard Java idiom.
There's still a way to pass information to this class as there is with
enums - add a mutator and some state for it to mutate.
--
Lew