Java Reflection - Class.getModifiers() Examples

Code Snippets Java Reflection Java 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public int getModifiers()
Returns:
the int representing the modifiers for this class

Examples


package com.logicbig.example.clazz;

import java.lang.reflect.Modifier;

public class GetModifiersExample {

public static void main(String... args) {
Class<GetModifiersExample> c = GetModifiersExample.class;
int modifiers = c.getModifiers();
String s = Modifier.toString(modifiers);
System.out.println(s);
}
}

Output

public




package com.logicbig.example.clazz;

import java.lang.reflect.Modifier;

public class GetModifiersExample2 {

public static void main(String... args) {
Class<MyInterface> c = MyInterface.class;
int m = c.getModifiers();
String s = Modifier.toString(m);
System.out.println(s);
}

private static interface MyInterface {}
}

Output

private abstract static interface




For arrays the modifiers are the same as of its component type.

package com.logicbig.example.clazz;

import java.lang.reflect.Modifier;

public class GetModifiersExample3 {

public static void main(String... args) {
Class<MyClass[]> c = MyClass[].class;
int m = c.getModifiers();
String s = Modifier.toString(m);
System.out.println(s);
}

private static class MyClass {}

}

Output

private abstract final




See Also