I have an enum and want to use a static value as the argument in the constructor.
public enum Enum
{
e1(0),
e2(1),
e3(SPECIAL_VALUE);
static int SPECIAL_VALUE = -1;
int value;
private Enum(int value)
{
this.value = value;
}
}
In this example, SPECIAL_VALUE
is accessed before it is initialized, so this clearly doesn't work. I was wondering if there was a common solution. Or a reason why I shouldn't need to do this.
NOTE: There's probably a duplicate out there somewhere, but everything I can find has to do with using a static field in the body of the constructor, not as an argument, and I don't think the solutions presented there are applicable.
package test;
public enum Enum
{
e1(0),
e2(1),
e3(SPECIAL_VALUE());
static int SPECIAL_VALUE(){return -1;}
int value;
private Enum(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public static void main(String args[])
{
System.out.println(e3.name());
System.out.println(e3.getValue());
}
}
tried ideone for the first time, voila! http://ideone.com/Bz1N69
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With