Listing the Modifiers of a Class Object in Java

Java 8Object Oriented ProgrammingProgramming

The modifiers for a class or an interface are returned by the java.lang.Class.getModifiers() method. These modifiers are encoded as an integer and they consist of the JVM’s constants for public, private, protected, final, abstract, static and interface.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.lang.reflect.Modifier;
public class Main {
   public static void main(String[] argv) throws Exception {
      int modifiers = String.class.getModifiers();
      String modifierString = Modifier.toString(modifiers);
      System.out.println("The Modifier is: " + modifierString);
   }
}

Output

The Modifier is: public final

Now let us understand the above program.

The method getModifiers() is used to obtain the modifiers for the String class. Then this is printed by using the method toString(). A code snippet which demonstrates this is as follows −

int modifiers = String.class.getModifiers();
String modifierString = Modifier.toString(modifiers);
System.out.println("The Modifier is: " + modifierString);
raja
Updated on 25-Jun-2020 13:50:34

Advertisements