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.
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.
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--