4

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";

}
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • 1
    The compiler cannot simply execute the name() method to know the value – Kumar Abhinav Aug 11 '14 at 22:24
  • What is your edit intending to show? `complete` is not a compile-time constant and as such cannot be used with annotation values. – Luke Woodward Aug 12 '14 at 07:27
  • 1
    @LukeWoodward you're right, wrong copy paste, my bad. I'm trying to show that you can still perform some computation – Eugene Aug 12 '14 at 08:04
  • @Eugene The above String variable "complete" is indeed compile time constant but You can use the variable value directly as compile time constant but not through a funtion even if it is a final method.In the above example,if I define a final method which returns the value of complete variable,then that method cannot act as a compile time constant.For enums,you would need to access the name variable directly and not name() method.I hope I am clear – Kumar Abhinav Aug 12 '14 at 12:17
  • @LukeWoodward complete is compile time constant.. – Kumar Abhinav Aug 12 '14 at 12:29
  • @KumarAbhinav I understand that and I know that it works like that - *no problem here*. What I do want to know is why there wasn't a part in the specification about enums in compile time constants. – Eugene Aug 12 '14 at 12:54
  • @Eugene Not just enums,it will not work for Strings also if you try to access a final String through a method – Kumar Abhinav Aug 12 '14 at 13:10
  • 1
    @KumarAbhinav yup, but enums are the obvious choice, cause they can't change. It probably has to do with the fact that the JVM compiles an enum to a class and method dispatching can't be computed to a compile time constant... – Eugene Aug 12 '14 at 13:11
  • @Eugene "Method dispatching can't be computed to a compile time constant"..true – Kumar Abhinav Aug 12 '14 at 13:14
  • 1
    @KumarAbhinav: it wasn't when I wrote that comment. See the edit history. – Luke Woodward Aug 12 '14 at 15:42
  • possible duplicate of [Compile time constants and variables](http://stackoverflow.com/questions/9082971/compile-time-constants-and-variables) – Kumar Abhinav Aug 12 '14 at 17:41
  • possible duplicate of [How to supply Enum value to an annotation from a Constant in Java](http://stackoverflow.com/questions/13253624/how-to-supply-enum-value-to-an-annotation-from-a-constant-in-java) – Martin Schröder Jul 31 '15 at 12:40

1 Answers1

2
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)

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • check my edit, it has nothing to do with "some computation". Also read the JLS part about compile time constants. – Eugene Aug 12 '14 at 05:57
  • I'm accepting and upvoting for your time, but this is, well, not what I expected.. Thx anyways. – Eugene Aug 12 '14 at 13:19
  • @Eugene I was unable to phrase the sentence"Method dispatching cannot be computed to a compile time constant".So u deserve it – Kumar Abhinav Aug 12 '14 at 13:25
  • @Eugene I think u shud see this link http://stackoverflow.com/questions/9082971/compile-time-constants-and-variables – Kumar Abhinav Aug 12 '14 at 18:30
  • @Eugene it is simply a case of it being a fixed constant but not falling under the definiton of "compile time constant" – Kumar Abhinav Aug 12 '14 at 18:57