Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 907 Bytes

05. Single Member Annotation.md

File metadata and controls

33 lines (24 loc) · 907 Bytes

Single Member Annotations

  • contains only one member
  • it is same as the other annotation except it provides a shorthand for specifying value of the member.
  • you can simply specify the value for that member without specifying the name.
  • but the member name must be value, it will not work for any other names
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface Description{
    String value();
}

class Main{
    @Description("This is the description for this method")
    public static void m(){}

    public static void main(String[] args){
        try{
            System.out.println(Main.class.getMethod("m").getAnnotation(Description.class)); // @Description("This is the description for this method")
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
    }
}