Package getAnnotationsByType() method in Java with Examples Last Updated : 03 Jan, 2020 Comments Improve Suggest changes Like Article Like Report The getAnnotationsByType() method of java.lang.Package class is used to get the annotations of the specified annotation type present in this class. The method returns an array of annotations for the specified annotation type. Syntax: public A[] getAnnotationsByType(Class<T> annotationClass) Parameter: This method accepts a parameter annotationClass which is the type of the annotation to get. Return Value: This method returns an array of annotations for the specified annotation type. Exception: This method throws: NullPointerException: if the given annotation class is null. Below programs demonstrate the getAnnotationsByType() method. Example 1: Java // Java program to demonstrate // getAnnotationsByType() method import java.util.*; import java.lang.annotation.*; // create a custom Annotation @Retention(RetentionPolicy.RUNTIME) @interface Annotation { // This annotation has two attributes. public String key(); public String value(); } // call Annotation for method // and pass values for annotation @Annotation(key = "GFG", value = "GeeksForGeeks") public class Test { public Object obj; public static void main(String[] args) throws ClassNotFoundException { // returns the Class object for this class Class myClass = Test.class; System.out.println( "Class represented by myClass: " + myClass.toString()); // Get the annotation // using getAnnotationsByType() method System.out.println( "Annotation of myClass of type Annotation: " + Arrays.toString( myClass .getAnnotationsByType( Annotation.class))); } } Output: Class represented by myClass: class Test Annotation of myClass of type Annotation: [@Annotation(key=GFG, value=GeeksForGeeks)] Example 2: Java // Java program to demonstrate // getAnnotationsByType() method import java.util.*; import java.lang.annotation.*; @Deprecated public class Test { public Object obj; public static void main(String[] args) throws ClassNotFoundException { try { // returns the Class object for this class Class myClass = Test.class; System.out.println( "Class represented by myClass: " + myClass.toString()); // Get the annotation // using getAnnotationsByType() method System.out.println( "Annotation of myClass of type Deprecated: " + Arrays.toString( myClass .getAnnotationsByType( Deprecated.class))); } catch (Exception e) { System.out.println(e); } } } Output: Class represented by myClass: class Test Annotation of myClass of type Deprecated: [@java.lang.Deprecated()] Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/Package.html#getAnnotationsByType-java.lang.Class- Comment More info G guptayashgupta53 Follow Improve Article Tags : Java Java-lang package Java-Functions java.lang.Package Class Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java5 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java5 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like