Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Using Reflection API to determine whether class object is an Annotation or not

Program to demonstrate how Reflection API can be used to determine whether a class object is an Annotation or not in Java

package com.hubberspot.reflection;

@interface Specifier {

}

class Public {

}

public class AnnotationInfo {

   public static void main(String[] args) {
   // Create a Test object
      Public publicInstance = new Public();
        
      // Get the class of respective types
      Class publicClass = publicInstance.getClass();
      Class specifier = Specifier.class;

      // isAnnotation() checks whether type is an annotation or not
      if(specifier.isAnnotation()) {
 // prints the simple name of the class
        System.out.println("Given type " + specifier.getSimpleName()
   + " is an Annotation");
      } else {
 System.out.println("Given class " + specifier.getSimpleName()
   + " is not an Annotation");
      }

      if(publicClass.isAnnotation()) {
 System.out.println("Given class " + publicClass.getSimpleName()
   + " is an Annotation");
      } else {
 System.out.println("Given class " + publicClass.getSimpleName()
   + " is not an Annotation");
      }


   }

}




Output of the program : 


 
© 2021 Learn Java by Examples Template by Hubberspot