public class MyClass { public int x; private int y; protected int z; } public class Main { public static void main(String[] args) { Class c = MyClass.class; int modifiers = c.getModifiers(); if (Modifier.isPublic(modifiers)) { System.out.println("MyClass is a public class"); } if (Modifier.isPrivate(modifiers)) { System.out.println("MyClass is a private class"); } if (Modifier.isProtected(modifiers)) { System.out.println("MyClass is a protected class"); } } }
abstract class MyAbstractClass { public abstract void myMethod(); } public class Main { public static void main(String[] args) { Class c = MyAbstractClass.class; int modifiers = c.getModifiers(); if (Modifier.isAbstract(modifiers)) { System.out.println("MyAbstractClass is an abstract class"); } } }This example uses getModifiers() method to check if a class is abstract using Modifier.isAbstract() method. These examples are part of the Java Standard Library.