Java: Can I use two different names in an enum to count as the same thing? -
i have enum class cardinal directions(north, east, south, west):
public enum direction { north, east, south, west; }
is there way able use multiple names same thing? example this:
public enum direction { north or n, east or e, south or s, west or w; }
in practice want able , sign variable either n or north , have 2 operations same.
example:
direction direction1=new direction.north; direction direction2=new direction.n; //direction1==direction2
public enum direction { north, east, south, west, ; // convenience names. public static final direction n = north; public static final direction e = east; public static final direction s = south; public static final direction w = west; }
is legal, "n"
not work auto-generated valueof
method. i.e. direction.valueof("n")
throw illegalargumentexception
instead of returning direction.north
.
you cannot write case n:
. have use full names in switch
es value direction
.
other that, abbreviated form should work full version. can use direction.n
in enumset
s, compare equality direction.n == direction.north
, name()
(which "north"
), import static yourpackage.direction.n;
, etc.
Comments
Post a Comment