The Wayback Machine - http://web.archive.org/web/20200420004441/https://www.geeksforgeeks.org/java-lang-class-class-java-set-2/amp/

Java.lang.Class class in Java | Set 2

Java.lang.Class class in Java | Set 1
More methods:

  1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine’s constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Modifier class in java.lang.Reflect package.
    Syntax : 
    public int getModifiers()
    Parameters : 
    NA
    Returns :
    the int representing the modifiers for this class
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getModifiers() method
      
    import java.lang.reflect.Modifier;
      
    public abstract class Test
    {
        public static void main(String[] args) 
        {
            // returns the Class object associated with Test class
            Class c = Test.class;
      
            // returns the Modifiers of the class Test
            // getModifiers method
            int i = c.getModifiers();
              
            System.out.println(i);
              
            System.out.print("Modifiers of " + c.getName() + " class are : ");
      
            // getting decoded i using toString() method
            // of Modifier class
            System.out.println(Modifier.toString(i));
        }
    }
    chevron_right
    
    

    Output:

    1025
    Modifiers of Test class are : public abstract
    
  2. T[] getEnumConstants() : This method returns the elements of this enum class. It returns null if this Class object does not represent an enum type.
    Syntax : 
    public T[] getEnumConstants()
    Parameters : 
    NA
    Returns :
    an array containing the values comprising the enum class represented by this Class object
    in the order they're declared,
    or null if this Class object does not represent an enum type
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getEnumConstants() method
      
    enum Color
    {
        RED, GREEN, BLUE;
    }
       
    public class Test
    {
        public static void main(String[] args) 
        {   
            // returns the Class object associated with Color(an enum class)
            Class c1 = Color.class;
              
            // returns the Class object associated with Test class
            Class c2 = Test.class;
              
            // returns the elements of Color enum class in an array
            // getEnumConstants method
            Object[] obj1 = c1.getEnumConstants();
              
            System.out.println("Enum constants of " + c1.getName() + " class are :");
              
            // iterating through enum constants
            for (Object object : obj1)
            {
                System.out.println(object);
            }
              
            // returns null as Test Class object does not represent an enum type
            Object[] obj2 = c2.getEnumConstants();
      
            System.out.println("Test class does not contain any Enum constant.");
            System.out.println(obj2);
              
        }
    }
    chevron_right
    
    

    Output:



    Enum constants of Color class are :
    RED
    GREEN
    BLUE
    Test class does not contain any Enum constant.
    null
    
  3. String getCanonicalName() : This method returns the canonical name of the underlying class as defined by the Java Language Specification.
    It returns null if the underlying class does not have a canonical name (i.e., if it is a local or anonymous class or an array whose component type does not have a canonical name).
    Syntax : 
    public String getCanonicalName()
    Parameters : 
    NA
    Returns :
    the Canonical name of the underlying class, if it exists
    null, otherwise
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getCanonicalName() method
    public class Test
    {
        public static void main(String[] args) 
                throws ClassNotFoundException 
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
      
            System.out.print("Canonical name of class represented by c1 : ");
              
            // returns the Canonical name of the class
            // getCanonicalName method
            System.out.println(c1.getCanonicalName());
        }
    }
    chevron_right
    
    

    Output:

    Canonical name of class represented by c1 : java.lang.String
    
  4. boolean desiredAssertionStatus() : This method returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
    Syntax : 
    public boolean desiredAssertionStatus()
    Parameters : 
    NA
    Returns :
    the desired assertion status of the specified class.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate desiredAssertionStatus() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
              
            // checking for assertion status of String class
              
            System.out.print("desired assertion status of " + c1.getName() + "class: ");
              
            // desiredAssertionStatus() method
            System.out.println(c1.desiredAssertionStatus());
              
        }
    }
    chevron_right
    
    

    Output:

    desired assertion status of java.lang.Stringclass : false
    
  5. Class<?> getComponentType() : This method returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.
    Syntax : 
    public Class<?> getComponentType()
    Parameters : 
    NA
    Returns :
    the Class representing the component type of this class if this class is an array
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getComponentType() method
    public class Test
    {
        public static void main(String[] args)
        {
            int a[] = new int[2];
              
            // returns the Class object for array class
            Class c = a.getClass();
              
            System.out.print("Component type of class represented by c : ");
              
            // getComponentType() method
            System.out.println(c.getComponentType());
              
        }
    }
    chevron_right
    
    

    Output:

    Component type of class represented by c : int
    
  6. Class<?>[] getDeclaredClasses() : Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
    This method includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces. This method returns an array of length 0 if the class declares no classes or interfaces as members, or if this Class object represents a primitive type, an array class, or void.
    Syntax : 
    public Class<?>[] getDeclaredClasses()
    Parameters : 
    NA
    Returns :
    the array of Class objects representing all the declared members of this class
    Throws :
    SecurityException - If a security manager, s, is present 
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getDeclaredClasses() method
      
    public class Test
    {
        // base interface
        interface A
        {
            // methods and constant declarations
        }
           
        // derived class
        class B implements A
        {
            // methods implementations that were declared in A
        }
      
          
        public static void main(String[] args)
        {
            // returns the Class object associated with Test class
            Class myClass = Test.class;
              
            // getDeclaredClasses on myClass
            // it returns array of classes and interface declare in Test class
            Class c[] = myClass.getDeclaredClasses();
              
            System.out.println("Declared classes and interfaces present in " + 
                                                      myClass.getName() + " class : ");
              
            // iterating through classes and interfaces declared in Test class
            for (Class class1 : c)
            {
                System.out.println(class1);
            }
              
        }
    }
    chevron_right
    
    

    Output:

    Declared classes and interfaces present in Test class : 
    interface Test$A
    class Test$B
    
  7. Field getDeclaredField(String fieldName) : This method returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
    The name parameter is a String that specifies the simple name of the desired field. Note that this method will not reflect the length field of an array class.
    Syntax : 
    public Field getDeclaredField(String fieldName) 
    throws NoSuchFieldException,SecurityException
    Parameters : 
    fieldName -  the field name
    Returns :
    the Field object for the specified field in this class
    Throws :
    NoSuchFieldException - if a field with the specified name is not found.
    NullPointerException - if fieldName is null
    SecurityException - If a security manager, s, is present.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getDeclaredField() method
      
    import java.lang.reflect.Field;
      
    public class Test
    {
        // any declared field
        int i;
          
        public static void main(String[] args) 
                throws NoSuchFieldException, SecurityException
        {
            // returns the Class object associated with Test class
            Class myClass = Test.class;
              
            // getDeclaredField on myClass
            Field f = myClass.getDeclaredField("i");
              
            System.out.println("Declared field present in " +
                                         myClass.getName() + 
                                         " class specified by \"i\" : ");
              
            System.out.println(f);
        }
    }
    chevron_right
    
    

    Output:

    Declared field present in Test class specified by "i" : 
    int Test.i
    
  8. Field[] getDeclaredFields() : This method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.

    This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.

    Syntax : 
    public Field[] getDeclaredFields()  throws SecurityException
    Parameters : 
    NA
    Returns :
    the array of Field objects representing all the declared fields of this class
    Throws :
    SecurityException - If a security manager, s, is present.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getDeclaredFields() method
      
    import java.lang.reflect.Field;
      
    public class Test
    {
        // some declared fields
        int i;
        String str;
        boolean b;
          
        public static void main(String[] args)
        {
            // returns the Class object associated with Test class
            Class myClass = Test.class;
              
            // getDeclaredFields on myClass
            Field f[] = myClass.getDeclaredFields();
              
            System.out.println("Declared fields present in " +
                                                myClass.getName() + " class are : ");
              
            // iterating through declared fields of Test class
            for (Field field : f) 
            {
                System.out.println(field);
            }
        }
    }
    chevron_right
    
    

    Output:



    Declared fields present in Test class are : 
    int Test.i
    java.lang.String Test.str
    boolean Test.b
    
  9. Method getDeclaredMethod(String methodName,Class… parameterTypes) : This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
    Syntax : 
    public Method getDeclaredMethod(String methodName,Class... parameterTypes) 
    throws NoSuchFieldException,SecurityException
    Parameters : 
    methodName -  the method name
    parameterTypes - the list of parameters
    Returns :
    the Method object for the method of this class matching the specified name and parameters
    Throws :
    NoSuchMethodException - if a method with the specified name is not found.
    NullPointerException - if methodName is null
    SecurityException - If a security manager, s, is present.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getDeclaredMethod() method
      
    import java.lang.reflect.Method;
      
    public class Test
    {
        // any declared method
        // with a String argument
        public void m1(String str)
        {
            System.out.println(str);
        }
          
        public static void main(String[] args) 
                throws NoSuchMethodException, SecurityException, ClassNotFoundException
        {
            // returns the Class object associated with Test class
            Class myClass = Test.class;
              
            // returns the Class object for the class 
            // with the specified name 
            Class c = Class.forName("java.lang.String");
              
            // getDeclaredMethod on myClass
            Method m = myClass.getDeclaredMethod("m1",c);
              
            System.out.println("Declared method present in " +
                                    myClass.getName() +
                                    " class specified by argument : " + c.getName());
              
            System.out.println(m);
        }
    }
    chevron_right
    
    

    Output:

    Declared method present in Test class specified by argument : java.lang.String
    public void Test.m1(java.lang.String)
    
  10. Method[] getDeclaredMethods() : This method retuns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private methods, but excludes inherited methods.

    This method returns an array of length 0 if the class or interface declares no methods, or if this Class object represents a primitive type, an array class, or void.

    Syntax : 
    public Method[] getDeclaredMethods() throws SecurityException
    Parameters : 
    NA
    Returns :
    the array of Method objects representing all the declared methods of this class
    Throws :
    SecurityException - If a security manager, s, is present.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getDeclaredMethods() method
      
    import java.lang.reflect.Method;
      
    public class Test
    {
        // some declared Methods
        public void m1()
        {
            System.out.println("Inside m1 method");
        }
          
        static void m2()
        {
            System.out.println("Inside m2 method");
        }
          
        // main method
        public static void main(String[] args)
        {
            // returns the Class object associated with Test class
            Class myClass = Test.class;
              
            // getDeclaredMethods on myClass
            Method m[] = myClass.getDeclaredMethods();
              
            System.out.println("Declared methods present in " +
                                                myClass.getName() + " class are : ");
              
            // iterating through declared Methods of Test class
            for (Method Method : m) 
            {
                System.out.println(Method);
            }
        }
    }
    chevron_right
    
    

    Output:

    Declared methods present in Test class are : 
    public static void Test.main(java.lang.String[])
    public void Test.m1()
    static void Test.m2()
    
  11. Constructor<?> getDeclaredConstructor(Class<?>… parameterTypes) : This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.
    Syntax : 
    public Constructor<?> getDeclaredConstructor(Class<?>... parameterTypes) 
    throws NoSuchMethodException,SecurityException
    Parameters : 
    parameterTypes - the list of parameters
    Returns :
    The Constructor object for the constructor with the specified parameter list
    Throws :
    NoSuchMethodException - if a Constructor with the specified parameterTypes is not found.
    SecurityException - If a security manager, s, is present.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getDeclaredConstructor() Constructor
      
    import java.lang.reflect.Constructor;
      
    public class Test
    {
          
        public static void main(String[] args) 
                throws NoSuchMethodException, SecurityException, ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.Integer");
            Class c2 = Class.forName("java.lang.String");
              
            // getDeclaredConstructor on myClass
            Constructor con = c1.getDeclaredConstructor(c2);
              
            System.out.println("Declared Constructor present in " + c1.getName() + 
                                      " class specified by argument : " + c2.getName());
              
            System.out.println(con);
        }
    }
    chevron_right
    
    

    Output:

    Declared Constructor present in java.lang.Integer class specified by argument : 
    java.lang.String public java.lang.Integer(java.lang.String) 
    throws java.lang.NumberFormatException
    
  12. Constructor<?>[] getDeclaredConstructors() : This method returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. These are public, protected, default (package) access, and private constructors.

    This method returns an array of length 0 if this Class object represents an interface, a primitive type, an array class, or void.

    Syntax : 
    public Constructor<?>[] getDeclaredConstructors() throws SecurityException
    Parameters : 
    NA
    Returns :
    the array of Constructor objects representing all the declared constructors of this class
    Throws :
    SecurityException - If a security manager, s, is present.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getDeclaredConstructors() Constructor
      
    import java.lang.reflect.Constructor;
      
    public class Test
    {
          
        public static void main(String[] args) 
                throws ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c = Class.forName("java.lang.String");
              
            // getDeclaredConstructors on myClass
            Constructor con[] = c.getDeclaredConstructors();
              
            System.out.println("Declared Constructors present in " +
                                    c.getName() + " class are : ");
              
            // iterating through all constructors
            for (Constructor constructor : con) 
            {
                System.out.println(constructor);
            }
        }
    }
    chevron_right
    
    

    Output:

    Declared Constructors present in java.lang.String class are : 
    public java.lang.String(byte[],int,int)
    public java.lang.String(byte[],java.nio.charset.Charset)
    public java.lang.String(byte[],java.lang.String) throws 
    java.io.UnsupportedEncodingException
    public java.lang.String(byte[],int,int,java.nio.charset.Charset)
    public java.lang.String(byte[],int,int,java.lang.String) 
    throws java.io.UnsupportedEncodingException
    java.lang.String(char[],boolean)
    public java.lang.String(java.lang.StringBuilder)
    public java.lang.String(java.lang.StringBuffer)
    public java.lang.String(byte[])
    public java.lang.String(int[],int,int)
    public java.lang.String()
    public java.lang.String(char[])
    public java.lang.String(java.lang.String)
    public java.lang.String(char[],int,int)
    public java.lang.String(byte[],int)
    public java.lang.String(byte[],int,int,int)
    
  13. Class<?> getDeclaringClass() : If the class or interface represented by this Class object is a member of another class, then this method returns the Class object representing the class in which it was declared.

    This method returns null if this class or interface is not a member of any other class. If this Class object represents an array class, a primitive type, or void,then this method returns null.

    Syntax : 
    public Class<?>  getDeclaringClass()
    Parameters : 
    NA
    Returns :
    the declaring class of this class
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate 
    // getDeclaringClass() Class
    import java.lang.reflect.Method;
    public class Test 
    {
        // any method
        public void m1()
        {
            System.out.println("Inside m1 method");
        }
          
        public static void main(String[] args) 
        {
              
            // returns A class object
            Class c1 = Test.class;
              
            // getting all methods of Test class
            // Note that methods from Object class
            // are also inherited
            Method m[] = c1.getMethods();
              
            for (Method method : m) 
            {
                // getDeclaringClass method
                // it return declared class of this method
                System.out.println(method.getName() + " is present in " 
                                                + method.getDeclaringClass());
            }
              
        }
    }
    chevron_right
    
    

    Output:

    main is present in class Test
    m1 is present in class Test
    wait is present in class java.lang.Object
    wait is present in class java.lang.Object
    wait is present in class java.lang.Object
    equals is present in class java.lang.Object
    toString is present in class java.lang.Object
    hashCode is present in class java.lang.Object
    getClass is present in class java.lang.Object
    notify is present in class java.lang.Object
    notifyAll is present in class java.lang.Object
    
  14. Class<?> getEnclosingClass() : This method returns the immediately enclosing class of the underlying class. If the underlying class is a top level class this method returns null.
    Syntax : 
    public Class<?> getEnclosingClass()
    Parameters : 
    NA
    Returns :
    the immediately enclosing class of the underlying class
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getEnclosingClass() Class
      
    public class Test
    {
        // any inner class of Test class
        class A{}
          
        public static void main(String[] args) 
        {
              
            // returns A class object
            Class c1 = Test.A.class;
              
            // getEnclosingClass method
            // it returns the class object where A class present
            Class c2 = c1.getEnclosingClass();
              
            System.out.println("The class " + c1.getName() + " is present in class : ");
              
            System.out.println(c2);
              
        }
    }
    chevron_right
    
    

    Output:

    The class Test$A is present in class : 
    class Test
    
  15. Method getEnclosingMethod() : If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.
    Syntax : 
    public Class<?> getEnclosingMethod()
    Parameters : 
    NA
    Returns :
    the immediately enclosing method of the underlying class,
    if that class is a local or anonymous class;
    otherwise null
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getEnclosingMethod() method
      
    import java.lang.reflect.Method;
      
    public class Test
    {
        // any method 
        public static Class m1()
        {   
            // any local class in m1
            class A{};
              
            // returning class A
            return A.class;
        }
      
          
        // main method
        public static void main(String[] args) 
                throws ClassNotFoundException
        {
            // returns A Class object
            Class c = Test.m1();
              
            // getEnclosingMethod method
            Method m = c.getEnclosingMethod();
              
            System.out.println("The class " + c.getName() + " is present in method : ");
              
            System.out.println(m);
              
        }
    }
    chevron_right
    
    

    Output:



    The class Test$1A is present in method : 
    public static java.lang.Class Test.m1()
    
  16. Constructor getEnclosingConstructor() : If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class. Returns null otherwise.
    Syntax : 
    public Constructor<?> getEnclosingConstructor()
    Parameters : 
    NA
    Returns :
    the immediately enclosing constructor of the underlying class,
    if that class is a local or anonymous class; 
    otherwise null.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate 
    // getEnclosingConstructor() Constructor
      
    import java.lang.reflect.Constructor;
      
    public class Test
    {
        Class c;
          
        // default(any) constructor 
        public Test()
        {
            // any local class
            class A{};
              
            // returns A class object
            c = A.class;
        } 
          
        public static void main(String[] args) 
        {
            // creating Test class object
            Test t = new Test();
              
            // getEnclosingConstructor method
            Constructor con = t.c.getEnclosingConstructor();
              
            System.out.println("The class " + t.c.getName() 
                            + " is present in constructor : ");
              
            System.out.println(con);
              
        }
    }
    chevron_right
    
    

    Output:

    The class Test$1A is present in Constructor : 
    public Test()
    
  17. ProtectionDomain getProtectionDomain() : Returns the ProtectionDomain of this class. If there is a security manager installed, this method first calls the security manager’s checkPermission method with a RuntimePermission(“getProtectionDomain”) permission to ensure it’s ok to get the ProtectionDomain.
    Syntax : 
    public ProtectionDomain getProtectionDomain()
    Parameters : 
    NA
    Returns :
    the ProtectionDomain of this class
    Throws :
    SecurityException - if a security manager s exists 
    and its checkPermission method doesn't allow getting the ProtectionDomain.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate getProtectionDomain() method
    public class Test
    {
        public static void main(String[] args)
                             throws ClassNotFoundException
        {
            // returns the Class object for the class 
            // with the specified name 
            Class c1 = Class.forName("java.lang.String");
              
            // checking for assertion status of String class
              
            System.out.println("protection domain of " + c1.getName() + " class : ");
              
            // getProtectionDomain() method
            // it will print null as String class is loaded by 
            // BootStrap class loader
            System.out.println(c1.getProtectionDomain());
              
        }
    }
    chevron_right
    
    

    Output:

    protection domain of java.lang.String class : 
    ProtectionDomain  null
     null
     
     java.security.Permissions@7852e922 (
     ("java.security.AllPermission" "" "")
    )
    
  18. boolean isAnnotationPresent(Class<?extends Annotation> annotationClass)() : This method returns true if an annotation for the specified type is present on this element, else false. This method is designed primarily for convenient access to marker annotations.
    Specified by:
    isAnnotationPresent in interface AnnotatedElement
    Syntax : 
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 
    Parameters : 
    annotationClass - the Class object corresponding to the annotation type
    Returns :
    true if an annotation for the specified annotation type is present on this element
    false, otherwise
    Throws:
    NullPointerException - if the given annotation class is null
    
  19. boolean isSynthetic() : This method determines if this class is a synthetic class or not.
    Syntax : 
    public boolean isSynthetic()
    Parameters : 
    NA
    Returns :
    return true if and only if this class is a synthetic class.
    
  20. URL getResource(String name) : Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.
    Syntax : 
    public URL getResource(String name)
    Parameters : 
    name - name of the desired resource
    Returns :
    A URL object or null if no resource with this name is found
    
  21. InputStream getResourceAsStream(String name) : Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.
    Syntax : 
    public InputStream getResourceAsStream(String name)
    Parameters : 
    name - name of the desired resource
    Returns :
    A InputStream object or null if no resource with this name is found
    Throws:
    NullPointerException - If name is null
    
  22. Object[] getSigners() : Gets the signers of this class.
    Syntax : 
    public Object[] getSigners()
    Parameters : 
    NA
    Returns :
    the signers of this class, or null if there are no signers.
    In particular,it returns null if this object represents a primitive type or void.
    
  23. Annotation[] getAnnotations() : This method returns all annotations present on this element. It returns an array of length zero if this element has no annotations.
    The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
    Specified by:
    getAnnotations() in interface AnnotatedElement
    Syntax : 
    public Annotation[] getAnnotations()
    Parameters : 
    NA
    Returns :
    all annotations present on this element
    
  24. <A extends Annotation> A getAnnotation(Class<A> annotationClass) : This method returns this element’s annotation for the specified type if such an annotation is present, else null.
    Specified by:
    getAnnotations() in interface AnnotatedElement
    Syntax : 
    public Annotation[] getAnnotations()
    Parameters : 
    annotationClass - the Class object corresponding to the annotation type
    Returns :
    return element's annotation for the specified annotation type if present on this element
    else null
    Throws:
    NullPointerException - if the given annotation class is null
    
  25. Annotation[] getDeclaredAnnotations() : Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. It returns an array of length zero if no annotations are directly present on this element.
    The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
    Specified by:
    getDeclaredAnnotations in interface AnnotatedElement
    Syntax : 
    public Annotation[] getDeclaredAnnotations()
    Parameters : 
    NA
    Returns :
    All annotations directly present on this element
    

This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Article Tags :
Practice Tags :