Search code examples
javaannotationsjls

Is it possible to specify default value for annotation field of another annotation type?


public @interface InnerAnnotation {
    String value() default "hello";
}

public @interface OuterAnnotation {
    InnerAnnotation value() default ???
}

And one more case:

public @interface AnotherOuterAnnotation {
    InnerAnnotation[] value() default ??? UPD: {} 
}

Solution

  • Yes, its possible:

    public @interface InnerAnnotation {
        String value() default "hello";
    }
    
    public @interface OuterAnnotation {
        InnerAnnotation value() default @InnerAnnotation(value = "Goodbye");
    }