Reputation: 120858
Suppose that I have a simple annotation:
@MyAnnotation(value=<SomeString>)
and an enum:
enum Days {
MONDAY...
}
I cant use this annotation like this:
@MyAnnotation(value=Days.MONDAY.name())
private class SomeClass {
//some code
}
This code will fail saying that "it must be a compiled time constant". I do understand why this happens and I am aware of the JSL part about compiled time constants.
My question is why and what is the reasoning behind not making an enum a compiled time constant according to the specification. It's not like you can change that enum name...
EDIT for Kumar
private static final class Test {
public static final String complete = "start" + "finish";
}
Upvotes: 4
Views: 1779
Reputation: 6675
Method dispatching cannot be computed to a compile time constant
For above example,I am giving an example as case in switch statements also require compile time constant
public class Joshua{
public final String complete = "start" + "finish";
public void check(String argument) {
switch(argument)
{
case complete: //This compiles properly
}
switch(argument)
{
case name(): //This doesn't compile
}
}
public final String name(){
return complete;
}
}
With final variables you know it is a compile time constant but methods are free to return anything(a final method simply cannot be overriden)
Upvotes: 2