Class Level Annotation


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Target({ElementType.TYPE}) //on class level
public @interface TestAnnotation
   String test();
   String createdBy() default "dsin";


test = "test",
createdBy = "dsin test"
public class TestExample {




if (obj.isAnnotationPresent(TestAnnotation.class)) {
   // way1:
   Annotation annotation = obj.getAnnotation(TestAnnotation.class);
   TestAnnotation testerInfo = (TestAnnotation) annotation;

   System.out.printf("%nTest :%s", testerInfo.test());
   System.out.printf("%nCreatedBy :%s", testerInfo.createdBy());
   // way2:

If we want to put value to an annotation, ( i.e. @TestAnnotation(“test”) ), just use value() method.

public @interface TestAnnotation
   String value();

ref :

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s