Course contentsShow
← Java
Lesson 382 of 2,8709. Static Members and Utility ClassesFree lesson

The Class Object: Representing Types at Runtime

Explore the Class<T> object that represents every type in Java and provides runtime type information.

The Class Object: Representing Types at Runtime

You'll learn: How Java uses special Class objects to represent every type in your program while it's running.

Every type in Java—whether String, int, or your own custom classes—has a corresponding Class object that represents it at runtime. Think of it like a backstage pass: while your objects are performing on stage, the Class object holds all the metadata about what type they are, what methods they have, and how they're structured.

What Is a Class Object?

A Class object is Java's way of representing type information as an actual object you can interact with. When your program runs, the JVM creates one Class object for each type that gets loaded. This means there's exactly one Class object for String, one for Integer, one for any class you've written, and so on.

How to Get a Class Object

You can obtain a Class object in three common ways:

  1. Using .class literal: Class<String> c = String.class;
  2. From an object instance: Class<?> c = myObject.getClass();
  3. By name: Class<?> c = Class.forName("java.lang.String"); (we'll explore this later)

What Can You Do With It?

The Class object lets you ask runtime questions like "What's this object's type?" or "What methods does this class have?" You can also use it to get the simple name of a class: String.class.getName() returns "java.lang.String".

This is the foundation for reflection—inspecting and manipulating types dynamically at runtime.

Key Takeaway: Every type in Java is represented at runtime by a Class object that holds metadata about that type and can be accessed using .class or .getClass().