The Class Class
The heart of Reflection — java.lang.Class, metadata vs data, one Class object per loaded class, how objects point to it, and who creates it.
#Introduction
Before learning the Reflection API, we must understand one important class: java.lang.Class. Every Reflection operation starts with it — without understanding Class, Reflection cannot be understood.
#What is the Class Class?
Many beginners think Class is a keyword. It is not. Class is a predefined final class in the package java.lang.
Since it belongs to java.lang, it is automatically imported into every Java program — we never write import java.lang.Class;.
#Why Do We Need the Class Class?
class Employee {
}Where does the JVM store information about Employee? Information like the class name, package name, methods, constructors, fields, interfaces, parent class, annotations and modifiers.
The JVM keeps this information after loading the class, and exposes it through an object of type Class.
#What is Metadata?
Metadata means data about data.
class Employee {
private String name;
private int age;
public void work() {
}
}The values Rahul and 25 are actual data. But this is metadata:
Class name = Employee
Number of fields = 2
Field names = name, age
Method = work()
Package, superclass, constructors, ...Reflection works on metadata, not actual business data.
Real-Life Analogy
A library book contains stories, examples and exercises — the actual data. Its cover page contains the book name, author, edition, publisher and price — the metadata.
Similarly, objects contain actual data; the Class object describes the metadata.
#How the JVM Creates It
The JVM loads the .class file into memory and, while loading, creates one Class object that gives access to all the metadata.
#One Class Object Per Loaded Class
For every loaded class, there is exactly one Class object in the JVM.
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();#What Information Does the Class Object Hold?
Class name, package name, constructors, methods, fields, interfaces, superclass, nested classes, enum constants, generic information, modifiers and annotations — everything related to the class structure.
It does not store object values.
class Employee {
String name;
int age;
public void work() {
}
}| The Class object knows | It does NOT know |
|---|---|
Class name → Employee | Rahul |
Package → com.demo | 25 |
Fields → name, age | (those belong to an object) |
Methods → work() | |
Constructor → Employee() |
#Relationship Between an Object and Its Class Object
Employee emp = new Employee();Can an object know which class created it? Yes. Every Java object internally keeps a reference to its class. That's why we can write:
emp.getClass();#Why Reflection Starts with Class
Suppose you want to know a class's methods, constructors, fields or annotations. Where is this information? Through the Class object. So Reflection first obtains the Class object; after that, everything becomes available.
#Important Methods of Class
We'll study these in detail later:
getName()
getSimpleName()
getPackageName() // Java 9+
getSuperclass()
getMethods()
getDeclaredMethods()
getFields()
getDeclaredFields()
getConstructors()
getDeclaredConstructors()
isInterface()
isAnnotation()
isEnum()
isArray()
isPrimitive()These methods help us inspect metadata.
#Is the Class Object Created by the Programmer?
No. The programmer never creates it.
new Class(); // ❌ impossibleThe constructor of Class is private. The JVM creates Class objects automatically while loading classes.
#Interview Traps
Q2Can two Employee objects have different Class objects?
No (for the same class loader). All of them return the same metadata object:
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
System.out.println(e1.getClass() == e2.getClass()); // true
System.out.println(e2.getClass() == e3.getClass()); // trueBecause there is only one Class<Employee> object.
#Key Points to Remember
Classis a predefined final class injava.lang.- Reflection starts with the
Classclass. - The
Classobject represents metadata — data about data. - One loaded class has only one
Classobject (per class loader). - Multiple objects share the same
Classobject. - The JVM creates
Classobjects automatically.
#Interview Questions
Q1What is the Class class?
A predefined final class in java.lang that represents the runtime metadata of a loaded Java class.
Q2What is metadata?
Information about the structure of a class, such as its name, methods, fields, constructors, annotations and superclass.
Q3How many Class objects are created for a class?
Only one for each loaded class (per class loader), regardless of how many objects of that class are instantiated.
Q4Does the Class object store object values?
No. It describes only class metadata, not instance data.
Q5Who creates the Class object?
The JVM, automatically, when it loads the class.
#Chapter Summary
Classis the gateway to Reflection.- Every loaded class has exactly one
Classobject. - The
Classobject holds metadata, not business data. - Every Java object internally knows its corresponding
Classobject. - All Reflection APIs operate on the
Classobject.