Java.lang.Class class in Java

  • August 29, 2022
  • Java
java thread class

Java includes a class called Class in the java.lang package. In a running Java application, instances of the class Class represent classes and interfaces. Class objects also represent the primitive Java types (boolean, byte, char, short, int, long, float, and double), as well as the keyword void. It has no public builder. The Java Virtual Machine creates class objects automatically (JVM). We cannot extend it because it is a final class.

Methods of java.lang.Class class:
forName():
public static Class forName(String className) throws ClassNotFoundException  
  • Returns the runtime Class descriptor for the specified Class.
  • For example, the following code fragment returns the runtime Class descriptor for the Class named java.lang.Thread:
		Class t = Class.forName("java.lang.Thread")
  • Parameters:class-Name – the fully qualified name of the desired Class
  • Throws: ClassNotFoundException If the Class could not be found.
 newInstance():
  public Object newInstance() throws InstantiationException, IllegalAccessException
  • Creates a new instance of this Class.
  • Returns: the new instance of this Class.
  • Throws: InstantiationException If you try to instantiate an abstract class or an interface, or if the instantiation fails for some other reason.
  • Throws: IllegalAccessException If the class or initializer is not accessible.
 getName():
  public String getName()
  • Returns the name of this Class.
getSuperclass():
  public Class getSuperclass()
  • Returns the superclass of this Class.
 getInterfaces():
  public Class[] getInterfaces()
  • Returns the interfaces of this Class. An array of length 0 is returned if this Class implements no interfaces.
 isInterface():
  public boolean isInterface()
  • Returns a boolean indicating whether this Class is an interface.
 toString():
  public String toString()
  • Returns the name of this Class or this interface.
  • The word “class” is prepended if it is a Class; the word “interface” is prepended if it is an interface.
  • Overrides: toString in class Object
getMethods() :
public Method[] getMethods() throws SecurityException
  • Returns: an array of Method objects representing public methods, as well as an array of length 0 if the class or interface has no accessible public methods, or if this Class object represents a primitive type or void.
  • Throws: SecurityException – If a security manager, s, is present.
getClassLoader() :
public ClassLoader getClassLoader()
  • 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 deny access to the class loader, this exception is thrown.
 getConstructors() :
public Constructor<?>[] getConstructors() throws SecurityException
  • Returns: an array of Constructor objects representing this class’s public constructors, and an array of length zero if the class or interface has no accessible public constructors, or if this Class object represents a primitive type or void.
  • Throws: SecurityException – If a security manager, s, is present, this exception is thrown.
Example: program to demonstrate forName() method

public class Coderz
{
	public static void main(String[] args)
						throws ClassNotFoundException
	{
		// forName method
		// it returns the Class object for the class
		// with the specified name
		Class c = Class.forName("java.lang.Math");
		
		System.out.print("Class represented by c : " + c.toString());
	}
}
Output:
Class represented by c : class java.lang.Math
Example: program to demonstrate isInstance() method

public class Coderz
{
	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");

		String s = "GeeksForGeeks";
		int i = 10;
		
		
		// checking for Class instance
		// isInstance method
		boolean b1 = c.isInstance(s);
		boolean b2 = c.isInstance(i);
		
		System.out.println("is s instance of String : " + b1);
		System.out.println("is i instance of String : " + b2);
		
	}
}
Output:
is s instance of String : true
is i instance of String : false
Example: program to demonstrate getPackage() method

public class Coderz
{
	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.Math");
		Class c2 = Class.forName("java.util.ArrayList");
		
		// getting package of class

		// getPackage method on c1
		System.out.println(c1.getPackage());

		// getPackage method on c2
		System.out.println(c2.getPackage());

	}
}
Output:
package java.lang
package java.util
Example: program to demonstrate getMethods() method

import java.lang.reflect.Method;

public class Coderz
{
	public static void main(String[] args)
			throws SecurityException,ClassNotFoundException
	{
		// returns the Class Math for the class
		// with the specified name
		Class c1 = Class.forName("java.lang.Math");
		
		// getMethods method on c1
		// it returns array of methods of Object class
		Method M[] = c1.getMethods();
		
		System.out.println("Below are the methods of Math class : ");
		
		// iterating through all methods of Math class
		for (Method method : M)
		{
		System.out.println(method);
		}
	}
}
Output:
Below are the methods of Math class : 
public static float java.lang.Math.abs(float)
public static long java.lang.Math.abs(long)
public static int java.lang.Math.abs(int)
public static double java.lang.Math.abs(double)
public static double java.lang.Math.sin(double)public static double java.lang.Math.cos(double)
public static double java.lang.Math.tan(double)
public static double java.lang.Math.atan2(double,double)
public static double java.lang.Math.sqrt(double)
public static double java.lang.Math.log(double)
public static double java.lang.Math.log10(double)
public static double java.lang.Math.pow(double,double)
public static double java.lang.Math.exp(double)
public static int java.lang.Math.min(int,int)
public static long java.lang.Math.min(long,long)
public static float java.lang.Math.min(float,float)
public static double java.lang.Math.min(double,double)
public static double java.lang.Math.max(double,double)
public static long java.lang.Math.max(long,long)
public static int java.lang.Math.max(int,int)
public static float java.lang.Math.max(float,float)
public static double java.lang.Math.floor(double)
public static double java.lang.Math.ceil(double)
public static double java.lang.Math.rint(double)
public static int java.lang.Math.addExact(int,int)
public static long java.lang.Math.addExact(long,long)
public static int java.lang.Math.decrementExact(int)
public static long java.lang.Math.decrementExact(long)
public static int java.lang.Math.incrementExact(int)
public static long java.lang.Math.incrementExact(long)
public static int java.lang.Math.multiplyExact(int,int)
public static long java.lang.Math.multiplyExact(long,int)
public static long java.lang.Math.multiplyExact(long,long)
public static long java.lang.Math.multiplyHigh(long,long)public static long java.lang.Math.negateExact(long)
public static int java.lang.Math.negateExact(int)
public static int java.lang.Math.subtractExact(int,int)
public static long java.lang.Math.subtractExact(long,long)
public static double java.lang.Math.fma(double,double,double)
public static float java.lang.Math.fma(float,float,float)
public static double java.lang.Math.copySign(double,double)
public static float java.lang.Math.copySign(float,float)
public static float java.lang.Math.signum(float)
public static double java.lang.Math.signum(double)
public static float java.lang.Math.scalb(float,int)
public static double java.lang.Math.scalb(double,int)
public static int java.lang.Math.getExponent(double)
public static int java.lang.Math.getExponent(float)
public static double java.lang.Math.asin(double)public static double java.lang.Math.acos(double)
public static double java.lang.Math.atan(double)
public static double java.lang.Math.toRadians(double)
public static double java.lang.Math.toDegrees(double)
public static double java.lang.Math.cbrt(double)
public static double java.lang.Math.IEEEremainder(double,double)
public static long java.lang.Math.round(double)
public static int java.lang.Math.round(float)
public static double java.lang.Math.random()public static int java.lang.Math.toIntExact(long)public static long java.lang.Math.multiplyFull(int,int)
public static int java.lang.Math.floorDiv(int,int)
public static long java.lang.Math.floorDiv(long,int)
public static long java.lang.Math.floorDiv(long,long)
public static int java.lang.Math.floorMod(int,int)
public static int java.lang.Math.floorMod(long,int)
public static long java.lang.Math.floorMod(long,long)
public static float java.lang.Math.ulp(float)
public static double java.lang.Math.ulp(double)
public static double java.lang.Math.sinh(double)
public static double java.lang.Math.cosh(double)
public static double java.lang.Math.tanh(double)
public static double java.lang.Math.hypot(double,double)
public static double java.lang.Math.expm1(double)
public static double java.lang.Math.log1p(double)
public static float java.lang.Math.nextAfter(float,double)
public static double java.lang.Math.nextAfter(double,double)
public static double java.lang.Math.nextUp(double)
public static float java.lang.Math.nextUp(float)
public static double java.lang.Math.nextDown(double)

Note: also read about the Reflection in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *