๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Class class in Reflection API  


Introduction
  • Class class in Java is part of the Reflection API and is used to represent the runtime metadata of a class or interface.
  • It is present in the java.lang package and is denoted as java.lang.Class<T>.
  • It allows you to get information about the class such as its fields, methods, constructors, modifiers, superclass and interfaces.
  • Objects of Class type are created automatically by JVM when a class is loaded.

Different Ways to get the Class object:
  • There are mainly three ways to get the Class object:
    1. Using Class.forName("fully.qualified.ClassName")
    2. Using object.getClass()
    3. Using ClassName.class
  • 1. Program Using Class.forName("fully.qualified.ClassName")
    public class MainApp
    {
        public static void main(String[] args) throws Exception
        {
            // Getting Class object using Class.forName()
            Class<?> c = Class.forName("java.lang.String");
    
            System.out.println("Class Name : " + c.getName());
            System.out.println("Is Interface : " + c.isInterface());
            System.out.println("Superclass : " + c.getSuperclass());
        }
    }
  • 2. & 3. Program Using object.getClass() and Using ClassName.class
    public class MainApp
    {
        public static void main(String[] args)
        {
            // ------ Using getClass() ------
            String str = "Hello";
            Class<?> c1 = str.getClass();
            System.out.println("Class from getClass(): " + c1.getName());
    
            // ------ Using .class ------
            Class<?> c2 = String.class;
            System.out.println("Class from .class: " + c2.getName());
        }
    }

Important Methods of Class class
  • Some important methods of Class class are as follows:
    S.No Method Use
    1 getName() Returns the fully qualified name of the class.
    2 getSimpleName() Returns the simple name of the class without package name.
    3 getSuperclass() Returns the superclass of the class.
    4 getInterfaces() Returns an array of interfaces implemented by the class.
    5 getDeclaredFields() Returns all fields (including private) declared in the class.
    6 getDeclaredMethods() Returns all methods (including private) declared in the class.
    7 getDeclaredConstructors() Returns all constructors (including private) declared in the class.
    8 getModifiers() Returns the modifiers (public, private, abstract, etc.) of the class as an int.
    9 isInterface() Checks if the Class object represents an interface.
    10 isArray() Checks if the Class object represents an array type.
    11 newInstance() Creates a new instance of the class (no-arg constructor only).
    12 getPackage() Returns the package of the class.
    13 getDeclaredAnnotations() Returns annotations present on the class.
  • Program:
    import java.lang.reflect.*;
    
    class Employee
    {
        public String name;
        private int salary;
    
        public Employee() {}
    
        public Employee(String name, int salary)
        {
            this.name = name;
            this.salary = salary;
        }
    
        public void work()
        {
            System.out.println(name + " is working");
        }
    
        private void secret()
        {
            System.out.println("Secret salary is: " + salary);
        }
    }
    
    public class MainApp3
    {
        public static void main(String[] args)
        {
            Class<?> clazz = Employee.class;
    
            // Print basic information
            System.out.println("Class Name: " + clazz.getName());
            System.out.println("Simple Name: " + clazz.getSimpleName());
            System.out.println("Superclass: " + clazz.getSuperclass().getName());
    
            // Print interfaces
            System.out.println("\n------ Interfaces ------");
            for (Class<?> i : clazz.getInterfaces())
            {
                System.out.println(i.getName());
            }
    
            // Print fields
            System.out.println("\n------ Declared Fields ------");
            for (Field f : clazz.getDeclaredFields())
            {
                System.out.println(f.getName() + " : " + f.getType().getSimpleName());
            }
    
            // Print methods
            System.out.println("\n------ Declared Methods------");
            for (Method m : clazz.getDeclaredMethods())
            {
                System.out.println(m.getName());
            }
    
            // Print constructors
            System.out.println("\n------ Declared Constructors ------");
            for (Constructor<?> c : clazz.getDeclaredConstructors())
            {
                System.out.println(c);
            }
        }
    }