is there a way I can specify my component's name by referring to the value in the enum?
No, there is not. If the annotation attribute was expecting an enum
, you could just use the enum
. But invoking a method does not resolve to a constant expression. You might think that you could make the field public
and access it directly
@Named(MY_ENUMS.A.name)
but that won't work either because MY_ENUMS.A.name
isn't a constant expression either.
The actual reason theat isn't a constant expression is that an enum constant is basically a variable. There is such a thing as a constant variable, which is a constant expression. For a variable to be a constant variable, it needs to be final
and initialized with a constant expression. An enum
constant is final
but is not initialized with a constant expression. Basically an enum
constant is compiled to
public static final YourEnum constant = new YourEnum();
The new YourEnum()
expression is not a constant expression. And therefore the constant is not a constant variable and can't be used to resolve a String
variable which it may be a constant variable.