Checking Class Properties with Modifiers
What you'll learn: How to programmatically determine if a class is public, abstract, final, or an interface using reflection modifiers.
The Core Idea
Every class in Java has modifiers that describe its properties—like whether it's public, abstract, final, or an interface. The Reflection API lets you inspect these properties at runtime by calling getModifiers() on a Class object, which returns an integer representing all the modifiers as bit flags. You then use static methods from the java.lang.reflect.Modifier class to decode those flags.
Think of modifiers as a checklist encoded in a single number. Instead of reading the source code, you're asking Java: "Is this class public? Is it final?" through reflection.
How It Works
Once you have a Class object (via .class, getClass(), or Class.forName()), call getModifiers() to get the encoded modifiers. Then use Modifier's static helper methods:
Class<?> clazz = String.class;
int mods = clazz.getModifiers();
if (Modifier.isPublic(mods)) {
System.out.println("String is public");
}
if (Modifier.isFinal(mods)) {
System.out.println("String is final");
}
if (Modifier.isAbstract(mods)) {
System.out.println("String is abstract");
}
if (Modifier.isInterface(mods)) {
System.out.println("String is an interface");
}
You can check any class—user-defined or from the JDK—to discover its compile-time characteristics dynamically.
Common Checks
Modifier.isPublic(mods)– Is the class accessible everywhere?Modifier.isAbstract(mods)– Can't be instantiated directly?Modifier.isFinal(mods)– Can't be subclassed?Modifier.isInterface(mods)– Is it an interface type?
Key Takeaway: Use getModifiers() plus java.lang.reflect.Modifier helper methods to programmatically inspect whether a class is public, abstract, final, or an interface—essential for runtime class analysis and framework logic.