Java Reflection – Modifier Example

You can gather access modifier details about a class, fields, and methods via the Class object. The table below shows a list of all modifier constants defined in Java. When you call the getModifiers() method, it will return to you an int where the bits are set accordingly.

In this example, I will show you how to use this method to get these details.


List of Modifiers

Num #Name
1ABSTRACT
2FINAL
3INTERFACE
4NATIVE
5PRIVATE
6PROTECTED
7PUBLIC
8STATIC
9STRICT
10SYNCHRONIZED
11TRANSIENT
12VOLATILE

We can inspect any class and get it’s modifiers using the getModifiers() method

Getting Modifiers for a given Class

Class<?> mc = Class.forName("com.avaldes.tutorials.Player");
int mods = mc.getModifiers();
System.out.println("Details for com.avaldes.tutorials.Player...");
System.out.println("  CLASS modifiers are " + Modifier.toString(mods));

Player Class Snippet

public final class Player {
  ...
}

Output

Details for com.avaldes.tutorials.Player...
  CLASS modifiers are public final

Getting all Methods and show their Access Modifiers

In this example, we will need to get all methods that are declared in a class and iterate through them. While iterating through each method we will need to call the getModifiers() method.

Class<?> mc = Class.forName("com.avaldes.tutorials.ModifierExample");
    
System.out.println("nDetails for com.avaldes.tutorials.ModifierExample...");
Method[] declaredMethods = mc.getDeclaredMethods();
for (Method method : declaredMethods) {
  int mods = method.getModifiers();
  System.out.println("  METHODS are " + method.getName() + "==> " + Modifier.toString(mods));
}

Output

Details for com.avaldes.tutorials.ModifierExample...
  METHODS are main==> public static
  METHODS are showAllModifiers==> public static
  METHODS are testClassMods==> public static
  METHODS are testFieldMods==> public static
  METHODS are returnInt==> public static
  METHODS are returnBoolean==> public static
  METHODS are testMethodMods==> public static


Getting all fields and show their Access Modifiers

For this next example, I created a FieldTestClass that contains a number of fields of different types and access modifiers. We will need to get all fields that are declared in the class and iterate through them. While iterating through each field we will need to call the getModifiers() method.

Class<?> mc = Class.forName("com.avaldes.tutorials.FieldTestClass");
    
System.out.println("nDetails for com.avaldes.tutorials.FieldTestClass...");
Field[] mf = mc.getDeclaredFields();
for (Field mf : mf) {
  int mods = field.getModifiers();
  System.out.println("  FIELDS are " + field.getName() + "==> " + Modifier.toString(mods));
}

Output

Details for com.avaldes.tutorials.FieldTestClass...
  FIELDS are MAXCOUNT==> public static final
  FIELDS are COUNT==> public static
  FIELDS are transientInt==> public transient
  FIELDS are privateInt==> private
  FIELDS are protectedInt==> protected
  FIELDS are publicBoolean==> public static
  FIELDS are volatileString==> public volatile

Full Program Listing (ModifierExample.java)

package com.avaldes.tutorials;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;

public class ModifierExample {

  public static void showAllModifiers(int mods) {
    System.out.println("  Is the class public? " + Modifier.isPublic(mods));
    System.out.println("  Is the class private? " + Modifier.isPrivate(mods));
    System.out.println("  Is the class protected? " + Modifier.isProtected(mods));
    System.out.println("  Is the class static? " + Modifier.isStatic(mods));
    System.out.println("  Is the class final? " + Modifier.isFinal(mods));
    System.out.println("  Is the class abstract? " + Modifier.isAbstract(mods));
    System.out.println("  Is the class Synchronized? " + Modifier.isSynchronized(mods));
    System.out.println("  Is the class Interface? " + Modifier.isInterface(mods));
    System.out.println("  Is the class Native? " + Modifier.isNative(mods));
    System.out.println("  Is the class Strict? " + Modifier.isStrict(mods));
    System.out.println("  Is the class Transient? " + Modifier.isTransient(mods));
    System.out.println("  Is the class Volatile? " + Modifier.isVolatile(mods));
  }
  
  public static void testClassMods() 
      throws ClassNotFoundException {
    Class<?> mc = Class.forName("com.avaldes.tutorials.Player");

    int mods = mc.getModifiers();
    
    System.out.println("Details for com.avaldes.tutorials.Player...");
    System.out.println("  CLASS modifiers are " + Modifier.toString(mods));
    System.out.println("nCheck individual Flags...");
    showAllModifiers(mods);
  }
  
  public static void testFieldMods() 
      throws ClassNotFoundException {
    
    Class<?> mc = Class.forName("com.avaldes.tutorials.FieldTestClass");
    
    System.out.println("nDetails for com.avaldes.tutorials.FieldTestClass...");
    Field[] mf = mc.getDeclaredFields();
    for (Field field : mf) {
      int mods = field.getModifiers();
      System.out.println("  FIELDS are " + field.getName() + "==> " + Modifier.toString(mods));
    }
  }
  
  // Dummy Method -- Justed added for more methods
  public static int returnInt() {
    int i = 0;
    
    return i;
  }

  // Dummy Method -- Justed added for more methods
  public static boolean returnBoolean() {
     return true;
  }

  public static void testMethodMods() 
      throws ClassNotFoundException {
    
    Class<?> mc = Class.forName("com.avaldes.tutorials.ModifierExample");
    
    System.out.println("nDetails for com.avaldes.tutorials.ModifierExample...");
    Method[] declaredMethods = mc.getDeclaredMethods();
    for (Method method : declaredMethods) {
      int mods = method.getModifiers();
      System.out.println("  METHODS are " + method.getName() + "==> " + Modifier.toString(mods));
    }
  }
  
  public static void main(String[] args) 
      throws ClassNotFoundException {
    
    testClassMods();
    testMethodMods();
    testFieldMods();
  }
}

class FieldTestClass {
  public static final int MAXCOUNT = 10;
  public static int COUNT = 0;
  public transient int transientInt = 0;
  private int privateInt = 999;
  protected int protectedInt = 0;
  public static boolean publicBoolean = false;
  public volatile String volatileString = "Hello World";
}

Full Program Listing (Player.java)

package com.avaldes.tutorials;

public final class Player {
  private String playerName;
  private int startYear;
  private String  team;
  
  public Player() { 
  }
  
  public Player(String name, int year, String team) {
    setPlayerName(name);
    setStartYear(year);
    setTeam(team);
  }
  
  public String getPlayerName() {
    return playerName;
  }
  public void setPlayerName(String playerName) {
    this.playerName = playerName;
  }
  public int getStartYear() {
    return startYear;
  }
  public void setStartYear(int startYear) {
    this.startYear = startYear;
  }
  public String getTeam() {
    return team;
  }
  public void setTeam(String team) {
    this.team = team;
  }
  
  @Override
    public String toString() {
       return playerName;
    }
}

Output

Details for com.avaldes.tutorials.Player...
  CLASS modifiers are public final

Check individual Flags...
  Is the class public? true
  Is the class private? false
  Is the class protected? false
  Is the class static? false
  Is the class final? true
  Is the class abstract? false
  Is the class Synchronized? false
  Is the class Interface? false
  Is the class Native? false
  Is the class Strict? false
  Is the class Transient? false
  Is the class Volatile? false

Details for com.avaldes.tutorials.ModifierExample...
  METHODS are main==> public static
  METHODS are showAllModifiers==> public static
  METHODS are testClassMods==> public static
  METHODS are testFieldMods==> public static
  METHODS are returnInt==> public static
  METHODS are returnBoolean==> public static
  METHODS are testMethodMods==> public static

Details for com.avaldes.tutorials.FieldTestClass...
  FIELDS are MAXCOUNT==> public static final
  FIELDS are COUNT==> public static
  FIELDS are transientInt==> public transient
  FIELDS are privateInt==> private
  FIELDS are protectedInt==> protected
  FIELDS are publicBoolean==> public static
  FIELDS are volatileString==> public volatile

Java Reflection Examples

  • Class Example
    Simple example shows you how to dynamically load and instantiate a class, get class name, package name, and superclass name.
  • Modifier Example
    Showing you how to get information about classes, fields and methods using modifier method.
  • Method Example
    Providing full details of how to get all methods, and invoke methods using reflection.

Frequently Asked Questions on Java Reflection API (FAQs)

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *