Class in Java Set -1

java.lang.Class

Instances of the class Class represent classes and interfaces in a running Java application. An enum type and a record type are classes; an annotation type is a kind of interface. Every array also belongs to a class that is reflected as a Class object shared by all arrays with the same element type and a number of dimensions. The primitive Java types(boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

The class has no public constructor. Instead, a Class object is constructed automatically by the Java Virtual 

The machine when a class is derived from the bytes of a class file through the invocation of one of the following methods:

1. ClassLoader::defineClass.

2. java.lang.invoke.MethodHandles.Lookup::defineClass.

3 java.lang.invoke.MethodHandles.Lookup::defineHiddenClass

The methods of class Class expose many characteristics of a class or interface. Most characteristics are derived from the class file that the class loader passed to the Java Virtual Machine or from the class file passed to Lookup::defineClassor Lookup::defineHiddenClass.A few characteristics are determined by the class loading environment at run time, such as the module returned by getModule().

Implemented Interfaces:

Serializable

AnnotatedElement

GenericDeclaration

Type

Some methods of Class

arrayType()This method returns a Class for an array type whose component type is described by this Class.

asSubclass(Class)This method casts this Class object to represent a subclass of the class represented by the specified class object.

cast(Object)This method casts an object to the class or interface represented by this Class object.

componentType()This method returns the component type of this Class, if it describes an array type, or null otherwise.

describeConstable()This method returns a nominal descriptor for this instance if one can be constructed, or an empty Optional if one cannot be.

descriptorString()This method returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

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.

Class.forName(String)This method returns the Class object associated with the class or interfaces with the given string name.

Class.forName(String, boolean, ClassLoader)his method returns the Class object associated with the class or interfaces with the given string name, using the given class loader.

getAnnotatedInterfaces()This method returns an array of AnnotatedType objects that represent the use of types to specify super interfaces of the entity represented by this Class object.

getAnnotatedSuperclass()This method returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.

getAnnotation(Class)This method returns this element's annotation for the specified type if such an annotation is present, else null.

getAnnotations()This method returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.

getAnnotationsByType(Class)This method returns annotations that are associated with this element.

getCanonicalName()This method returns the canonical name of the underlying class as defined by The Java Language Specification.

getClasses()This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

getClassLoader()This method returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader.

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.

getConstructor(Class...)This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

getConstructors()This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

getDeclaredAnnotation(Class)This method takes one argument. This method returns this element's annotation for the specified type if such an annotation is directly present, else null.

getDeclaredAnnotations()This method returns annotations that are directly present on this element. This method ignores inherited annotations. 

getDeclaredAnnotationsByType(Class)This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

getDeclaredClasses()This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

getDeclaredConstructor(Class...)This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.


Class in Java Set -1

Class in Java Set -2

Class in Java Set -3

Class getDeclaredConstructor(Class...) in Java

getDeclaredConstructor(Class...): This method is available in the java.lang.Class of Java.

Syntax:

Constructor java.lang.Class.getDeclaredConstructor(Class... parameterTypes) throws NoSuchMethodException, SecurityException

This method takes one argument. This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

The parameterTypes parameter is an array of Class objects that identify the constructor's formal parameter types, in declared order. If this Class object represents an inner class declared in a non-static context, the formal parameter types include the explicit enclosing instance as the first parameter.

Parameters: One parameter is required for this method.

parameterTypes: the parameter array.

Returns: The Constructor object for the constructor with the specified parameter list.

Throws:

1. NoSuchMethodException - if a matching method is not found.

2. SecurityException - If a security manager, s, is present and any of the following conditions are met:

a). the caller's class loader is not the same as the class loader of this class and invocation of the s.checkPermission method with RuntimePermission("accessDeclaredMembers")denies access to the declared constructor.

b). the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class.

Approach 1: When no exception

Java

package com.Class;

public class ClassgetDeclaredConstructor {

    @SuppressWarnings("unchecked")
    public static void main(String[] args)
throws ClassNotFoundException,
NoSuchMethodException, SecurityException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.String");

        @SuppressWarnings("rawtypes")
        Class parameterTypes = Class.forName("java.lang.String");

        System.out.println(class1.getDeclaredConstructor(parameterTypes));
    }
}

Output:

public java.lang.String(java.lang.String)


Approach 2: NoSuchMethodException

Java

package com.Class;

public class ClassgetDeclaredConstructor {

    @SuppressWarnings("unchecked")
    public static void main(String[] args)
throws ClassNotFoundException,
NoSuchMethodException, SecurityException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.String");

        @SuppressWarnings("rawtypes")
        Class parameterTypes = Class.forName("java.lang.Thread");

        System.out.println(class1.getDeclaredConstructor(parameterTypes));
    }
}

Output:

Exception in thread "main" java.lang.NoSuchMethodException: java.lang.String.<init>(java.lang.Thread) at java.base/java.lang.Class.getConstructor0(Class.java:3508) at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2711) at com.Class.ClassgetDeclaredConstructor.main(ClassgetDeclaredConstructor.java:14)


Some other methods of Class

arrayType()This method returns a Class for an array type whose component type is described by this Class.

asSubclass(Class)This method casts this Class object to represent a subclass of the class represented by the specified class object.

cast(Object)This method casts an object to the class or interface represented by this Class object.

componentType()This method returns the component type of this Class, if it describes an array type, or null otherwise.

describeConstable()This method returns a nominal descriptor for this instance if one can be constructed, or an empty Optional if one cannot be.

descriptorString()This method returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

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.

Class.forName(String)This method returns the Class object associated with the class or interfaces with the given string name.

Class.forName(String, boolean, ClassLoader)his method returns the Class object associated with the class or interfaces with the given string name, using the given class loader.

getAnnotatedInterfaces()This method returns an array of AnnotatedType objects that represent the use of types to specify super interfaces of the entity represented by this Class object.

getAnnotatedSuperclass()This method returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.

getAnnotation(Class)This method returns this element's annotation for the specified type if such an annotation is present, else null.

getAnnotations()This method returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.

getAnnotationsByType(Class)This method returns annotations that are associated with this element.

getCanonicalName()This method returns the canonical name of the underlying class as defined by The Java Language Specification.

getClasses()This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

getClassLoader()This method returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader.

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.

getConstructor(Class...)This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

getConstructors()This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

getDeclaredAnnotation(Class)This method takes one argument. This method returns this element's annotation for the specified type if such an annotation is directly present, else null.

getDeclaredAnnotations()This method returns annotations that are directly present on this element. This method ignores inherited annotations. 

getDeclaredAnnotationsByType(Class)This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

getDeclaredClasses()This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

getDeclaredConstructor(Class...)This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

Class getDeclaredClasses() in Java

getDeclaredClasses(): This method is available in the java.lang.Class of Java.

Syntax:

Class[] java.lang.Class.getDeclaredClasses() throws SecurityException

This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

This includes public, protected, default(package) access, and private classes and interfaces declared by the class but exclude 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.

Parameters: NA

Returns: the array of Class objects representing all the declared members of this class.

Throws:

1. SecurityException If a security manager, s, is present and any of the following conditions is met:

a). the caller's class loader is not the same as the class loader of this class and invocation of the s.checkPermission method with RuntimePermission("accessDeclaredMembers")denies access to the declared classes within this class.

b) the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class.

Approach

Java

package com.Class;

import java.util.Arrays;

public class ClassgetDeclaredClasses {

    public static void main(String[] args)
throws ClassNotFoundException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.String");

        System.out.println(Arrays.toString(class1.getDeclaredClasses()));
    }
}

Output:

[class java.lang.String$CaseInsensitiveComparator]


Some other methods of Class

arrayType()This method returns a Class for an array type whose component type is described by this Class.

asSubclass(Class)This method casts this Class object to represent a subclass of the class represented by the specified class object.

cast(Object)This method casts an object to the class or interface represented by this Class object.

componentType()This method returns the component type of this Class, if it describes an array type, or null otherwise.

describeConstable()This method returns a nominal descriptor for this instance if one can be constructed, or an empty Optional if one cannot be.

descriptorString()This method returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

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.

Class.forName(String)This method returns the Class object associated with the class or interfaces with the given string name.

Class.forName(String, boolean, ClassLoader)his method returns the Class object associated with the class or interfaces with the given string name, using the given class loader.

getAnnotatedInterfaces()This method returns an array of AnnotatedType objects that represent the use of types to specify super interfaces of the entity represented by this Class object.

getAnnotatedSuperclass()This method returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.

getAnnotation(Class)This method returns this element's annotation for the specified type if such an annotation is present, else null.

getAnnotations()This method returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.

getAnnotationsByType(Class)This method returns annotations that are associated with this element.

getCanonicalName()This method returns the canonical name of the underlying class as defined by The Java Language Specification.

getClasses()This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

getClassLoader()This method returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader.

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.

getConstructor(Class...)This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

getConstructors()This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

getDeclaredAnnotation(Class)This method takes one argument. This method returns this element's annotation for the specified type if such an annotation is directly present, else null.

getDeclaredAnnotations()This method returns annotations that are directly present on this element. This method ignores inherited annotations. 

getDeclaredAnnotationsByType(Class)This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

getDeclaredClasses()This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

getDeclaredConstructor(Class...)This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

Class getDeclaredAnnotationsByType(Class) in Java

getDeclaredAnnotationsByType(Class): This method is available in the java.lang.Class of Java.

Syntax:

A[] java.lang.Class.getDeclaredAnnotationsByType(Class<A> annotationClass)

This method takes one argument. This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

This method ignores inherited annotations.

If there are no specified annotations directly or indirectly present on this element, the return value is an array of length 0.

Type Parameters: <A> the type of the annotation to query for and return if directly or indirectly present.

Parameters: One parameter is required for this method.

annotationClass: the Class object corresponding to the annotation type.

Returns: all this element's annotations for the specified annotation type if directly or indirectly present on this element, else an array of length zero.

Throws:

1. NullPointerException - if the given annotation class is null.

Approach 1: When no exception

Java

package com.Class;

import java.util.Arrays;

public class ClassgetDeclaredAnnotationsByType {

    @SuppressWarnings("unchecked")
    public static void main(String[] args)
throws ClassNotFoundException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.annotation.Annotation");

        @SuppressWarnings("rawtypes")
        Class class2 = Class.forName("java.lang.annotation.Annotation");

        System.out.println(Arrays.toString(class1.getDeclaredAnnotationsByType(class2)));
    }
}

Output:

[]


Approach 2: NullPointerException 

Java

package com.Class;

import java.util.Arrays;

public class ClassgetDeclaredAnnotationsByType {

    @SuppressWarnings("unchecked")
    public static void main(String[] args)
throws ClassNotFoundException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.annotation.Annotation");

        @SuppressWarnings("rawtypes")
        Class class2 = null;

        System.out.println(Arrays.toString(class1.getDeclaredAnnotationsByType(class2)));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.lang.Class.getDeclaredAnnotationsByType(Class.java:3936) at com.Class.ClassgetDeclaredAnnotationsByType.main(ClassgetDeclaredAnnotationsByType.java:16)


Some other methods of Class

arrayType()This method returns a Class for an array type whose component type is described by this Class.

asSubclass(Class)This method casts this Class object to represent a subclass of the class represented by the specified class object.

cast(Object)This method casts an object to the class or interface represented by this Class object.

componentType()This method returns the component type of this Class, if it describes an array type, or null otherwise.

describeConstable()This method returns a nominal descriptor for this instance if one can be constructed, or an empty Optional if one cannot be.

descriptorString()This method returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

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.

Class.forName(String)This method returns the Class object associated with the class or interfaces with the given string name.

Class.forName(String, boolean, ClassLoader)his method returns the Class object associated with the class or interfaces with the given string name, using the given class loader.

getAnnotatedInterfaces()This method returns an array of AnnotatedType objects that represent the use of types to specify super interfaces of the entity represented by this Class object.

getAnnotatedSuperclass()This method returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.

getAnnotation(Class)This method returns this element's annotation for the specified type if such an annotation is present, else null.

getAnnotations()This method returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.

getAnnotationsByType(Class)This method returns annotations that are associated with this element.

getCanonicalName()This method returns the canonical name of the underlying class as defined by The Java Language Specification.

getClasses()This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

getClassLoader()This method returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader.

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.

getConstructor(Class...)This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

getConstructors()This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

getDeclaredAnnotation(Class)This method takes one argument. This method returns this element's annotation for the specified type if such an annotation is directly present, else null.

getDeclaredAnnotations()This method returns annotations that are directly present on this element. This method ignores inherited annotations. 

getDeclaredAnnotationsByType(Class)This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

getDeclaredClasses()This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

getDeclaredConstructor(Class...)This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.

Class getDeclaredAnnotations() in Java

getDeclaredAnnotations(): This method is available in the java.lang.Class of Java.

Syntax:

Annotation[] java.lang.Class.getDeclaredAnnotations()

This method returns annotations that are directly present on this element. This method ignores inherited annotations. If there are no annotations directly present on this element, the return value is an array of length 0. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.

Parameters: NA

Returns: annotations directly present on this element.

Exceptions: NA

Approach

Java

package com.Class;

import java.util.Arrays;

public class ClassgetDeclaredAnnotations {

    public static void main(String[] args)
throws ClassNotFoundException {

        @SuppressWarnings("rawtypes")
        Class class1 = Class.forName("java.lang.Thread");

        System.out.println(Arrays.toString(class1.getDeclaredAnnotations()));
    }
}

Output:

[]


Some other methods of Class

arrayType()This method returns a Class for an array type whose component type is described by this Class.

asSubclass(Class)This method casts this Class object to represent a subclass of the class represented by the specified class object.

cast(Object)This method casts an object to the class or interface represented by this Class object.

componentType()This method returns the component type of this Class, if it describes an array type, or null otherwise.

describeConstable()This method returns a nominal descriptor for this instance if one can be constructed, or an empty Optional if one cannot be.

descriptorString()This method returns the descriptor string of the entity (class, interface, array class, primitive type, or void) represented by this Class object.

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.

Class.forName(String)This method returns the Class object associated with the class or interfaces with the given string name.

Class.forName(String, boolean, ClassLoader)his method returns the Class object associated with the class or interfaces with the given string name, using the given class loader.

getAnnotatedInterfaces()This method returns an array of AnnotatedType objects that represent the use of types to specify super interfaces of the entity represented by this Class object.

getAnnotatedSuperclass()This method returns an AnnotatedType object that represents the use of a type to specify the superclass of the entity represented by this Class object.

getAnnotation(Class)This method returns this element's annotation for the specified type if such an annotation is present, else null.

getAnnotations()This method returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.

getAnnotationsByType(Class)This method returns annotations that are associated with this element.

getCanonicalName()This method returns the canonical name of the underlying class as defined by The Java Language Specification.

getClasses()This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.

getClassLoader()This method returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader.

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.

getConstructor(Class...)This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

getConstructors()This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

getDeclaredAnnotation(Class)This method takes one argument. This method returns this element's annotation for the specified type if such an annotation is directly present, else null.

getDeclaredAnnotations()This method returns annotations that are directly present on this element. This method ignores inherited annotations. 

getDeclaredAnnotationsByType(Class)This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

getDeclaredClasses()This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.

getDeclaredConstructor(Class...)This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.