java custom annotation: make an attribute optional

flybywire picture flybywire · Aug 31, 2009 · Viewed 59.5k times · Source

I defined my own custom annotation

@Target(value={ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation  {
    Class<?> myType();
}

how, if at all, can I make the attribute optional

Answer

Dan Dyer picture Dan Dyer · Aug 31, 2009

You can provide a default value for the attribute:

@Target(value={ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation  {
    Class<?> myType() default Object.class;
}