Java.lang Package Classes

Java Class - getModifiers() Method



The java.lang.Class.getModifiers() method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface; they should be decoded using the methods of class Modifier.

Syntax

public int getModifiers()

Parameters

No parameter is required.

Return Value

Returns the int representing the modifiers for this class.

Exception

NA.

Example:

The example below shows usage of the java.lang.Class.getModifiers() method.

import java.lang.*;
import java.lang.reflect.*;

public class MyClass {
  public static void main(String[] args) {
    MyClass x = new MyClass();
    Class cls = x.getClass();

    //print the modifier for the class
    int i = cls.getModifiers();
    String name = Modifier.toString(i);
    System.out.println("Class Modifier = " + name);
  }
}

The output of the above code will be:

Class Modifier = public

❮ Java.lang - Class