Skip to content

Latest commit

 

History

History
36 lines (24 loc) · 851 Bytes

File metadata and controls

36 lines (24 loc) · 851 Bytes

Default values

  • we can provide default values to the annotations if no values are provided.
  • this can be done by adding a default clause to a member's declaration.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    String str() default "This is default value";
    int value() default 24;
}

class Main {
    
    @MyAnnotation
    public static void say(String message, int repeatTimes){}

    @MyAnnotation(str = "override the message")
    public static void say1(String message, int repeatTimes){}

    @MyAnnotation(value = 55)
    public static void say2(String message, int repeatTimes){}

    @MyAnnotation(str = "hello", value = 67)
    public static void hello(){}

    public static void main(String[] args){
    }

}