Method Class | getModifiers() method in Java
Last Updated :
05 Dec, 2018
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.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = demo. class ;
Method[] methods = classobj.getMethods();
Method setValueMethodObject = methods[ 0 ];
int modifier = setValueMethodObject.getModifiers();
System.out.println( "Modifier of setValue():" );
System.out.println(Modifier.toString(modifier));
}
catch (Exception e) {
e.printStackTrace();
}
}
class demo {
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.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = democlass. class ;
Method[] methods = classobj.getMethods();
for (Method method : methods) {
int modifier = method.getModifiers();
System.out.println( "Modifier of method name:"
+ method.getName());
System.out.println(Modifier.toString(modifier));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class democlass {
public static int method1()
{
System.out.println( "setValue" );
return 24 ;
}
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–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...