Error setting a default null value for an annotation's field

ripper234 picture ripper234 · Jul 24, 2009 · Viewed 53.3k times · Source

Why am I getting an error "Attribute value must be constant". Isn't null constant???

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SomeInterface {
    Class<? extends Foo> bar() default null;// this doesn't compile
}

Answer

Logan Murphy picture Logan Murphy · Apr 24, 2013

Try this

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SomeInterface {
    Class bar() default void.class;
}

It does not require a new class and it is already a keyword in Java that means nothing.