Method Class | getModifiers() method in Java
Last Updated :
11 Jul, 2025
The
java.lang.reflect.Method.getModifiers() method of Method class returns the modifiers for the method represented by this Method object. It returns int value. Then with the use Modifier class, the name of modifiers corresponding to that value is fetched.
Syntax:
public int getModifiers()
Return Value: This method returns the
modifiers for the method represented by this Method object.
Below programs illustrates getModifiers() method of Method class:
Program 1: This program prints modifier of method when applying getModifiers() on a method object of class.
Java
// Program Demonstrate how to apply getModifiers() method
// of Method Class.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class GFG {
// Main method
public static void main(String[] args)
{
try {
// create class object
Class classobj = demo.class;
// get object of setValue() method of demo class
Method[] methods = classobj.getMethods();
Method setValueMethodObject = methods[0];
// get modifier of setValueMethodObject
int modifier = setValueMethodObject.getModifiers();
// print modifier details
System.out.println("Modifier of setValue():");
System.out.println(Modifier.toString(modifier));
}
catch (Exception e) {
e.printStackTrace();
}
}
// sample class contains method with public modifier
class demo {
// method has public modifier
public int setValue()
{
System.out.println("setValue");
return 24;
}
}
}
Output:
Modifier of setValue():
public
Program 2: Program demonstrates how to apply getModifiers() method of Method Class. Program prints Modifier Names for all the methods of democlass class.
Java
// Program Demonstrate how to apply getModifiers() method
// of Method Class.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class GFG {
// Main method
public static void main(String[] args)
{
try {
// create class object
Class classobj = democlass.class;
// get list of methods of democlass
Method[] methods = classobj.getMethods();
// loop through methods list
for (Method method : methods) {
// get Modifier of current method
int modifier = method.getModifiers();
// print method name
System.out.println("Modifier of method name:"
+ method.getName());
// print Modifier details
System.out.println(Modifier.toString(modifier));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
// a simple class
class democlass {
// method has static modifier
public static int method1()
{
System.out.println("setValue");
return 24;
}
// method has public final modifier
public final String method2()
{
System.out.println("getValue");
return "getValue";
}
}
Output:
Modifier of method name:method1
public static
Modifier of method name:method2
public final
Modifier of method name:wait
public final
Modifier of method name:wait
public final native
Modifier of method name:wait
public final
Modifier of method name:equals
public
Modifier of method name:toString
public
Modifier of method name:hashCode
public native
Modifier of method name:getClass
public final native
Modifier of method name:notify
public final native
Modifier of method name:notifyAll
public final native
Explanation: Output of this program also showing results for method objects other than methods defined in class object like wait, equals, toString, hashCode, getClass, notify, notifyAll.These methods are inherited from superclass name Object of java.lang lang package by class object.
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getModifiers--
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java