Constructor getAnnotation() method in Java with Examples Last Updated : 27 Nov, 2019 Comments Improve Suggest changes Like Article Like Report The getAnnotation() method of a Constructor class is used to get this constructor object annotation for the specified type if such an annotation is present, else null. Specified type is passed as parameter. Syntax: public <T extends Annotation> T getAnnotation(Class<T> annotationClass) Parameters: This method accepts a single parameter annotationClass which represents the Class object corresponding to the annotation type. Return value: This method returns this element's annotation for the specified annotation type if present on this element, else null. Exception: This method throws NullPointerException if the given annotation class is null. Below programs illustrate the getAnnotation() method: Program 1: Java // Java program to demonstrate // Constructor.getAnnotation() method import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedType; import java.lang.reflect.Constructor; public class GFG { public static void main(String... args) throws NoSuchMethodException { // Create Constructor Object Constructor[] constructors = Demo.class.getConstructors(); // Create annotation object Annotation annotation = constructors[0] .getAnnotation(PathVar.class); if (annotation instanceof PathVar) { PathVar customAnnotation = (PathVar)annotation; System.out.println( "Path: " + customAnnotation.Path()); } } } // Demo class class Demo { public Demo(@PathVar(Path = "Demo/usr") String str) {} } // PathVar interface @Target({ ElementType.TYPE_USE }) @Retention(RetentionPolicy.RUNTIME) @interface PathVar { public String Path(); } Output: Path: Demo/usr Program 2: Java // Java program to demonstrate // Constructor.getAnnotation() method import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Constructor; public class GFG { public static void main(String... args) throws NoSuchMethodException { // Create Constructor Object Constructor[] constructors = Maths.class.getConstructors(); // Create annotation object Annotation annotation = constructors[0] .getAnnotation(Calculator.class); System.out.println( "Annotation:" + annotation.toString()); } } // Demo class @Calculator(add = "Adding value", subtract = "Subtracting Value") class Maths { @Calculator(add = "Adding value", subtract = "Subtracting Value") public Maths() {} } // Calculator interface @Retention(RetentionPolicy.RUNTIME) @interface Calculator { public String add(); public String subtract(); } Output: Annotation : @Calculator(add=Adding value, subtract=Subtracting Value) References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getAnnotation(java.lang.Class) Comment More infoAdvertise with us Next Article Constructor getAnnotation() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Constructors Java-Functions java-lang-reflect-package Practice Tags : Java Similar Reads Constructor getDeclaredAnnotations() method in Java with Examples The getDeclaredAnnotations() method of java.lang.reflect.Constructor class is used to return annotations present on this Constructor element as an array of Annotation class objects. The getDeclaredAnnotations() method ignores inherited annotations on this constructor object.Returned array can contai 2 min read Constructor getParameterAnnotations() method in Java with Examples The getParameterAnnotations() method of a Constructor class is used to get a two-dimensional Annotation array that represent the annotations on the formal parameters of this Constructor. If the Constructor contains no parameters, an empty array will be returned. If the Constructor contains one or mo 2 min read Constructor getName() method in Java with Examples The constructor class provides information about a single constructor for a class and it also provides access to that constructor. The getName() method of java.lang.reflect.Constructor is used to return this constructor name, as a string. Constructor name is the binary name of the constructor's decl 1 min read Constructor getAnnotatedReturnType() method in Java with Examples The getAnnotatedReturnType() method of a Constructor class is used to return an AnnotatedType object which represents AnnotatedType to specify return type of Constructor Object. The returned AnnotatedType represents implementation of AnnotatedType itself or any of its sub-interfaces like AnnotatedAr 2 min read Class getAnnotation() method in Java with Examples The getAnnotation() method of java.lang.Class class is used to get the annotation of the specified annotation type, if such an annotation is present in this class. The method returns that class in the form of an object. Syntax: public T getAnnotation(Class<T> annotationClass) Parameter: This m 2 min read Like