Listing the Modifiers of a Class Object in Java


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);

Updated on: 25-Jun-2020

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements