101

I use annotation in my code, and I try to use value which determine in run time.

I define my list as static final (lst), and I add to this list some elements.

When I use lst.get(i), I get compilation error:

The value for annotation attribute must be a constant expression

What is the solution for this issue?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cons
  • 1,133
  • 3
  • 8
  • 8
  • 2
    Use a constant expression. – Dave Newton May 12 '13 at 15:46
  • You can schedule things a different way: http://stackoverflow.com/questions/7814089/how-to-schedule-a-periodic-task-in-java – obesechicken13 Dec 14 '15 at 16:57
  • downvoting for no code and duplication – Barett Dec 31 '18 at 20:50
  • 2
    it is nonsense restriction; just because it was implemented that way does not mean it is the right way. perhaps they may have wanted to go in phases and first implemented compile time validation constraints, but that does not mean we must stop there and crib about its inflexibility. I think it is time we raise noise about the need for run time validation of constraints, even if it incurs a small performance hit. those constraints that have compile time values must be validated using compile time validator implementations and those that have run time values must be validated using runtime valid – Saasira Jul 11 '15 at 09:33
  • I cannot see why this question was marked as a duplication. They are related but not duplicated. This question is more specific. If it was not with this title, I would not have thought the answers to the other question is helpful. – Devs love ZenUML Aug 24 '19 at 04:21

2 Answers2

66

The value for an annotation must be a compile time constant, so there is no simple way of doing what you are trying to do.

See also here: How to supply value to an annotation from a Constant java

It is possible to use some compile time tools (ant, maven?) to config it if the value is known before you try to run the program.

mbmc
  • 5,024
  • 5
  • 25
  • 53
zw324
  • 26,764
  • 16
  • 85
  • 118
  • 3
    > It is possible to use some compile time tools (ant, maven?) to config it if the value is known before you try to run the program. How? – mdzh Mar 02 '21 at 09:14
29

This is what a constant expression in Java looks like:

package com.mycompany.mypackage;

public class MyLinks {
  // constant expression
  public static final String GUESTBOOK_URL = "/guestbook";
}

You can use it with annotations as following:

import com.mycompany.mypackage.MyLinks;

@WebServlet(urlPatterns = {MyLinks.GUESTBOOK_URL})
public class GuestbookServlet extends HttpServlet {
  // ...
}
Benny Code
  • 51,456
  • 28
  • 233
  • 198
  • 2
    what about `GUESTBOOK_URL = "book" + MyEnum.Field` , is that possible? – G. Ciardini Sep 01 '21 at 16:08
  • 3
    the meaning of 'constant' in the context of the question is compile time constant. `final` is generally not compile time constant. This won't work for a List. The code is misleading, for it only works, because the string in the class is a compile time constant. – johannes_lalala May 11 '22 at 23:07