Apparently, null is not a constant

· Java

... Or at least that's what the compiler claims. Check out the following code for a simple annotation:
22   @Documented 
23   @Inherited 
24   @Target({ElementType.FIELD, ElementType.METHOD}) 
25   @Retention(RetentionPolicy.RUNTIME) 
26   public @interface Sequence { 
27       Class<? extends Collection> type() default null; 
28   } 
29   
The compiler gives an error claming that the default cannot be 'null', as it must be a constant value. Now, I ask you: What is null if not the mother of all constants?

Comments (3)

Problems with WeakHashMap at Chaotic Java
[...] « Apparently, null is not a constant [...]
Zoran Rilak
Heh. I ran into the exact same issue, myself: @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Handle { String value() default ""; } Another curious thing: if you try int x = (int)null; the error you get is "Cannot cast from null to int", meaning that null is a *type* as well as that type's singleton value.
Zoran Rilak
*EDIT* I mistakenly posted the code that does work, above. :)