Modifier

In this chapter you will learn:

  1. How to get information for a modifier with java.lang.reflect.Modifier

Get modifier from java.lang.reflect.Modifier

The java.lang.reflect.Modifier class provides static methods and constants to decode class and member access modifiers.

The following list contains constants defined in java.lang.reflect.Modifier. Each constant represents one type of modifier.

  • static int ABSTRACT represents the abstract modifier.
  • static int FINAL represents the final modifier.
  • static int INTERFACE represents the interface modifier.
  • static int NATIVE represents the native modifier.
  • static int PRIVATE represents the private modifier.
  • static int PROTECTED represents the protected modifier.
  • static int PUBLIC represents the public modifier.
  • static int STATIC represents the static modifier.
  • static int STRICT represents the strictfp modifier.
  • static int SYNCHRONIZED represents the synchronized modifier.
  • static int TRANSIENT represents the transient modifier.
  • static int VOLATILE represents the volatile modifier.

We can check the modifier type by using the following method.

  • static boolean isAbstract(int mod) Is abstract modifier.
  • static boolean isFinal(int mod) Is final modifier.
  • static boolean isInterface(int mod) Is interface modifier.
  • static boolean isNative(int mod) Is native modifier.
  • static boolean isPrivate(int mod) Is private modifier.
  • static boolean isProtected(int mod) Is protected modifier.
  • static boolean isPublic(int mod) Is public modifier.
  • static boolean isStatic(int mod) Is static modifier.
  • static boolean isStrict(int mod) Is strictfp modifier.
  • static boolean isSynchronized(int mod) Is synchronized modifier.
  • static boolean isTransient(int mod) Is transient modifier.
  • static boolean isVolatile(int mod) Is volatile modifier.
  • static String toString(int mod) Convert modifier int flag to string.

The following code uses the static method from Modifier to check if a class is a public class.

import java.lang.reflect.Modifier;
//from  jav  a  2 s  . c  o  m
public class Main {
  public static void main(String[] args) throws Exception {
    getClassModifier(String.class);
  }

  private static void getClassModifier(Class clazz) {
    int modifier = clazz.getModifiers();

    if (Modifier.isPublic(modifier)) {
      System.out.println(clazz.getName() + " class modifier is public");
    }
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to get package for a name
  2. How to get the package name
  3. How to get implementation title, vendor and version
  4. How to get the specification title, vendor and version
  5. How to get all packages
  6. How get the annotations for a package
Home » Java Tutorial » Reflection
Reflection
Class Reflection
Class modifier, package and string presentation
Constructor reflection
Field Reflection
Get/Set field value
Java method reflection
Modifier
Package
Array reflection
Get annotation type
Method annotation