The enum value used in the annotation where the string is expected

Suppose I have a simple annotation:

 @MyAnnotation(value=<SomeString>)

      

and enumeration:

 enum Days {
      MONDAY...
 }

      

I cannot use this annotation like this:

 @MyAnnotation(value=Days.MONDAY.name())
 private class SomeClass {
       //some code
 }

      

This code will not be able to say "it should be a compiled time constant". I understand why this is happening, and I am aware of the JSL part about compiled time constants.

My question is why and why the argument doesn't make the correspondence compiled time constant as per spec. It doesn't look like you can change this enum name ...

EDIT for Kumar

private static final class Test {

    public static final String complete = "start" + "finish";

}

      

+3


source to share


1 answer


Method dispatching cannot be computed to a compile time constant

      

In the above example I gave an example how the case in switch statements also requires constant compile time



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, which you know are compile-time constant, but methods are free to return (the final method simply cannot be overridden)

+2


source







All Articles