Java Program to Check an Annotation Type Using the IsAnnotation() Method

Java program to check an annotation type using the isAnnotation() method

// Java program to check an annotation type 
// using isAnnotation() method

@interface A {}
public class Main {
  public static void main(String[] args) throws ClassNotFoundException {
    Class cls1 = A.class;
    Class cls2 = Main.class;

    if (cls1.isAnnotation())
      System.out.println("The cls1 is representing an annotation type");
    else
      System.out.println("The cls1 is not representing an annotation type");

    if (cls2.isAnnotation())
      System.out.println("The cls2 is representing an annotation type");
    else
      System.out.println("The cls2 is not representing an annotation type");
  }
}

Output

The cls1 is representing an annotation type
The cls2 is not representing an annotation type
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment