![]() |
VOOZH | about |
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are constructed automatically by the Java Virtual Machine(JVM). It is a final class, so we cannot extend it. The Class class methods are widely used in Reflection API.
In Java, the java.lang.Class class is a built-in class that represents a class or interface at runtime. It contains various methods that provide information about the class or interface, such as its name, superclass, interfaces, fields, and methods.
Output:
Name: java.lang.String Simple name: String Superclass: class java.lang.Object Interfaces: [] Is string assignable from Object? true
Creating a Class object
There are three ways to create Class object :
Class c = Class.forName(String className)
Class c = int.class
A a = new A(); // Any class A Class c = A.class; // No error Class c = a.class; // Error
A a = new A(); // Any class A Class c = a.getClass();
Syntax : public String toString() Parameters : NA Returns : a string representation of this class object. Overrides : toString in class Object
Class represented by c1: class java.lang.String Class represented by c2: int Class represented by c3: void
Syntax : public static Class<?> forName(String className) throws ClassNotFoundException Parameters : className - the fully qualified name of the desired class. Returns : return the Class object for the class with the specified name. Throws : LinkageError : if the linkage fails ExceptionInInitializerError - if the initialization provoked by this method fails ClassNotFoundException - if the class cannot be located
Output:
Class represented by c : class java.lang.String
Class<?> forName(String className,boolean initialize, ClassLoader loader) : This method also returns the Class object associated with the class or interface with the given string name using the given class loader. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader in. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.
Syntax : public static Class<?> forName(String className,boolean initialize, ClassLoader loader) throws ClassNotFoundException Parameters : className - the fully qualified name of the desired class initialize - whether the class must be initialized loader - class loader from which the class must be loaded Returns : return the Class object for the class with the specified name. Throws : LinkageError : if the linkage fails ExceptionInInitializerError - if the initialization provoked by this method fails ClassNotFoundException - if the class cannot be located
Example
Output:
Class represented by c : class java.lang.String
T newInstance() : This method creates a new instance of the class represented by this Class object. The class is created as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.
Syntax : public T newInstance() throws InstantiationException,IllegalAccessException TypeParameters : T - The class type whose instance is to be returned Parameters : NA Returns : a newly allocated instance of the class represented by this object. Throws : IllegalAccessException - if the class or its nullary constructor is not accessible. InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void or if the class has no nullary constructor; or if the instantiation fails for some other reason. ExceptionInInitializerError - if the initialization provoked by this method fails. SecurityException - If a security manager, s, is present
Output:
Class of obj : class Test
boolean isInstance(Object obj) : This method determines if the specified Object is assignment-compatible with the object represented by this Class. It is equivalent to instanceof operator in java.
Syntax : public boolean isInstance(Object obj) Parameters : obj - the object to check Returns : true if obj is an instance of this class else return false
Output:
is s instance of String : true is i instance of String : false
boolean isAssignableFrom(Class<?> cls) : This method determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface, of the class or interface represented by the specified Class parameter.
Syntax : public boolean isAssignableFrom(Class<?> cls) Parameters : cls - the Class object to be checked Returns : true if objects of the type cls can be assigned to objects of this class Throws: NullPointerException - if the specified Class parameter is null.
Output:
is Thread class Assignable from Test : true is String class Assignable from Test : false
boolean isInterface() : This method determines if the specified Class object represents an interface type.
Syntax : public boolean isInterface() Parameters : NA Returns : return true if and only if this class represents an interface type else return false
Output:
is java.lang.String an interface : false is java.lang.Runnable an interface : true
boolean isPrimitive() : This method determines if the specified Class object represents a primitive type.
Syntax : public boolean isPrimitive() Parameters : NA Returns : return true if and only if this class represents a primitive type else return false
Output:
is int primitive : true is class Test primitive : false
boolean isArray() : This method determines if the specified Class object represents an array class.
Syntax : public boolean isArray() Parameters : NA Returns : return true if and only if this class represents an array type else return false
is class [I an array : true is class Test an array : false
Syntax : public boolean isAnonymousClass() Parameters : NA Returns : true if and only if this class is an anonymous class. false,otherwise.
Syntax : public boolean isLocalClass() Parameters : NA Returns : true if and only if this class is a local class. false,otherwise.
Syntax : public boolean isMemberClass() Parameters : NA Returns : true if and only if this class is a Member class. false,otherwise.
Syntax : public boolean isEnum() Parameters : NA Returns : true if this class was declared as an enum in the source code. false,otherwise.
is class Color an Enum class : true is class Test an Enum class : false
Syntax : public boolean isAnnotation() Parameters : NA Returns : return true if and only if this class represents an annotation type else return false
is interface A an annotation : true is class Test an annotation : false
Syntax : public String getName() Parameters : NA Returns : returns the name of the name of the entity represented by this object.
Class Name associated with c : Test
Syntax : public String getSimpleName() Parameters : NA Returns : the simple name of the underlying class
Class Name associated with c : java.lang.String Simple class Name associated with c : String
Syntax : public ClassLoader getClassLoader() Parameters : NA Returns : the class loader that loaded the class or interface represented by this object represented by this object. Throws : SecurityException - If a security manager and its checkPermission method denies access to the class loader for the class.
Test class loader : sun.misc.Launcher$AppClassLoader@73d16e93 String class loader : null primitive int loader : null
Syntax : public TypeVariable<Class<T>>[] getTypeParameters() Specified by: getTypeParameters in interface GenericDeclaration Parameters : NA Returns : an array of TypeVariable objects that represent the type variables declared by this generic declaration represented by this object. Throws : GenericSignatureFormatError - if the generic signature of this generic declaration does not conform to the format specified in JVM.
TypeVariables in java.util.Set class : E
Syntax : public Class<? super T> getSuperclass() Parameters : NA Returns : the superclass of the class represented by this object
Test superclass : class java.lang.Object A superclass : class java.lang.Object B superclass : class A Object superclass : null
Syntax : public Type getGenericSuperclass() Parameters : NA Returns : the superclass of the class represented by this object Throws: GenericSignatureFormatError - if the generic class signature does not conform to the format specified in JVM TypeNotPresentException - if the generic superclass refers to a non-existent type declaration MalformedParameterizedTypeException - if the generic superclass refers to a parameterized type that cannot be instantiated for any reason
Test superclass : class java.lang.Object Set superclass : java.util.AbstractList<E> Object superclass : null
Syntax : public Class<?>[] getInterfaces() Parameters : NA Returns : an array of interfaces implemented by this class.
interfaces implemented by B class : interface A interfaces implemented by String class : interface java.io.Serializable interface java.lang.Comparable interface java.lang.CharSequence
Syntax : public Type[] getGenericInterfaces() Parameters : NA Returns : an array of interfaces implemented by this class Throws: GenericSignatureFormatError - if the generic class signature does not conform to the format specified in JVM TypeNotPresentException - if the generic superinterfaces refers to a non-existent type declaration MalformedParameterizedTypeException - if the generic superinterfaces refers to a parameterized type that cannot be instantiated for any reason
interfaces implemented by Set interface : java.util.Collection<E>
Syntax : public Package getPackage() Parameters : NA Returns : the package of the class, or null if no package information is available from the archive or codebase.
package java.lang, Java Platform API Specification, version 1.8 package java.util, Java Platform API Specification, version 1.8
Syntax : public Field[] getFields() throws SecurityException Parameters : NA Returns : the array of Field objects representing the public fields and array of length 0 if the class or interface has no accessible public fields or if this Class object represents a primitive type or void. Throws : SecurityException - If a security manager, s, is present.
Below are the fields of Integer class : public static final int java.lang.Integer.MIN_VALUE public static final int java.lang.Integer.MAX_VALUE public static final java.lang.Class java.lang.Integer.TYPE public static final int java.lang.Integer.SIZE public static final int java.lang.Integer.BYTES
Syntax : Class<?>[ ] getClasses() Parameters : NA Returns : the array of Class objects representing the public members of this class Throws : SecurityException - If a security manager, s, is present.
public members of Test class : interface Test$A class Test$B
Syntax : public Method[] getMethods() throws SecurityException Parameters : NA Returns : the array of Method objects representing the public methods and array of length 0 if the class or interface has no accessible public method or if this Class object represents a primitive type or void. Throws : SecurityException - If a security manager, s, is present.
Below are the methods of Object class : public final void java.lang.Object.wait() throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public java.lang.String java.lang.Object.toString() public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll()
Syntax : public Constructor<?>[] getConstructors() throws SecurityException Parameters : NA Returns : the array of Constructor objects representing the public constructors of this class and array of length 0 if the class or interface has no accessible public constructor or if this Class object represents a primitive type or void. Throws : SecurityException - If a security manager, s, is present.
Below are the constructors of Boolean class : public java.lang.Boolean(boolean) public java.lang.Boolean(java.lang.String)
Syntax : public Field getField(String fieldName) throws NoSuchFieldException,SecurityException Parameters : fieldName - the field name Returns : the Field object of this class specified by name 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.
public field in Integer class with MIN_VALUE name : public static final int java.lang.Integer.MIN_VALUE
Syntax : public Method getMethod(String methodName,Class... parameterTypes) throws NoSuchFieldException,SecurityException Parameters : methodName - the method name parameterTypes - the list of parameters Returns : the method object of this class specified by name 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.
public method in Integer class specified by parseInt : public static int java.lang.Integer.parseInt(java.lang.String) throws java.lang.NumberFormatException
Syntax : public Constructor<?> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException,SecurityException Parameters : parameterTypes - the list of parameters Returns : the Constructor object of the public constructor that matches the specified parameterTypes Throws : NoSuchMethodException - if a Constructor with the specified parameterTypes is not found. SecurityException - If a security manager, s, is present.
public Constructor in Integer class with String parameterType : public java.lang.Integer(java.lang.String) throws java.lang.NumberFormatException
Syntax : public T cast(Object obj) TypeParameters : T - The class type whose object is to be cast Parameters : obj - the object to be cast Returns : the object after casting, or null if obj is null Throws : ClassCastException - if the object is not null and is not assignable to the type T.
class A class B
class A class B
Syntax : public <U> Class<? extends U> asSubclass(Class<U> class) TypeParameters : U - The superclass type whose object is to be cast Parameters : clazz - the superclass object to be cast Returns : this Class object, cast to represent a subclass of the specified class object. Throws : ClassCastException - if this Class object does not represent a subclass of the specified class (here "subclass" includes the class itself).
class B
class B