What is the Class Object?
You'll learn: What the Class object is and how it provides runtime metadata about every Java type.
The Core Idea
In Java, every type—whether it's a class like String, an interface like List, a primitive like int, or even an array—has an associated Class object that lives in memory at runtime. This special object contains metadata about that type: its name, methods, fields, constructors, annotations, and more.
Think of the Class object as a "blueprint catalog card" at runtime. While your class definition in a .java file is the blueprint itself, the Class object is Java's runtime registry entry that describes everything about that blueprint—without needing the source code.
Why It Matters
When your program runs, Java loads class definitions into memory. For each type, it creates exactly one Class instance that represents it. This object enables:
- Reflection: Inspecting and manipulating types dynamically
- Type checking: Verifying what something is at runtime
- Dynamic instantiation: Creating objects without compile-time knowledge of the type
For example, String.class is a Class object representing the String type. You can ask it questions like "What methods do you have?" or "What's your fully qualified name?" without ever looking at the String source code.
The Singleton Nature
Each type has exactly one Class object per classloader. If you access String.class multiple times, you always get the same object—it's a singleton for that type.
Key Takeaway: Every type in Java has a unique Class object at runtime that stores comprehensive metadata about that type, enabling reflection and dynamic type operations.