Is it possible to define something like this in java?
C# code:
public enum Character
{
A = 1,
B = 2,
C = 4,
D = 8
}
...
Character ch = /* from user */
if(ch & Character.A)
{
// some operation...
}
for example if ch set to Character.B then result of if will be false:
ch = 00000000 00000000 00000000 00000010
A = 00000000 00000000 00000000 00000001
------------------------------------------
& 00000000 00000000 00000000 00000000
I want to implement something like that! Is it ever possible in Java?
Well, you can nearly do that:
Then:
This can be useful in some situations, but as Michael said you should definitely look at
EnumSetfor general “set of values” options.If you do decide to go for an enum with values, you can put extra logic within the enum itself: