Method Class | getModifiers() method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
        }
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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";
    }
}

chevron_right


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–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.