Annotation paremeter being constant as infinal static not enouh ?.

Why is it that being constant as in final static is not 'enough' constant

for Annotation parameters ?.

example:

the first version of maxlength makes @Column compile fine,

but the second while it compiles fine for the maxlength decleration itself , the @Column does fail with 'attribute value must be constant'.

static final maxlength = 20;

static final maxlength = myEnum.liten.anFinalint;

@Column(length=maxlength)

private String blabla;

im using jdk 1.6 btw.

It would be really convenient if the requirements for being 'constant' would be the same for annotation parameters as for normal constants like static final .

is there a good technical reason for annotations being harsher ?.

regards

gustav

[792 byte] By [fsdfsdf234234sdf23a] at [2007-11-26 17:03:50]
# 1
I'd say it's because a non-static variable (e.g. within an enum) is not constant at compile time. The static final member will be constant at runtime once the class is loaded/initialized, if necessary loading/initializing all dependent classes like the enum type.
stefan.schulza at 2007-7-8 23:31:36 > top of Java-index,Core,Core APIs...
# 2

the runtime class loading and init has nothing to do with this.

one of the main points with Enum is that it gives compile time type safety, i fail to see what about Enum that is not constant and available at compile time ?.

final int maxlength = myEnum.liten.an_Final_int;

compiles, hence the compiler has proven for itself that its indeed constant. else the usage of 'final' would fail here, but it does not.

so how come that Annotations usage is not satisfied ?

to me it seems logicaly flawed that Annotations is working this way.

I want to be able to use Enums for nice OO solutions , it really is a great tool, but this Annotation limit prevents me from doing it a nice way.

fsdfsdf234234sdf23a at 2007-7-8 23:31:36 > top of Java-index,Core,Core APIs...
# 3

You are mixing compile-time constants with static and final members. Neither means that a variable is a compile-time constant.

Defining static just says, that "there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created." (JLS)

Further, declaring a field final only says that it "may only be assigned to once." (JLS) Thus, it is guaranteed, that final fields are runtime-constants.

Compile-time constants are values, that always are guaranteed to not fail being created. As enums are instances (even final ones), their construction may fail (imagine having a variable which is initialized to a class that does not exist in the latter execution path), their instance variables are no constants.

See also: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313

stefan.schulza at 2007-7-8 23:31:36 > top of Java-index,Core,Core APIs...
# 4
your right, thanks stefan.
fsdfsdf234234sdf23a at 2007-7-8 23:31:36 > top of Java-index,Core,Core APIs...