Method Class | getParameterCount() method in Java

The java.lang.reflect.Method.getParameterCount() method of Method class returns the number of parameters declared on a method Object.

Syntax:

public int getParameterCount()

Return Value: This method returns number of formal parameters defined on this Method Object.



Below programs illustrates getParameterCount() method of Method class:

Example 1: Below Program returns No of Parameter for a specific Method given as Input.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program Demonstrate how to apply getParameterCount()
// method of Method Class.
  
import java.lang.reflect.Method;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class classobj = GFG.class;
  
            // get list of methods
            Method[] methods = classobj.getMethods();
  
            // get no of parameters for method setManyValues
            int noOfParameters = methods[0].getParameterCount();
  
            System.out.println("Method Name: "
                               + methods[0].getName());
  
            // print no of parameters
            System.out.println("No of parameters:" + noOfParameters);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    // method name setManyValues
    // No of parameters is one for this method
    public void setManyValues(String parameter1)
    {
        System.out.println("setManyValues");
    }
}

chevron_right


Output:

Method Name: setManyValues
No of parameters:1

Example 2: Below Program return No of Parameters for all of Methods Defined in a class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program Demonstrate how to apply getParameterCount() method
// of Method Class.
  
import java.lang.reflect.Method;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class classobj = DemoClass.class;
  
            // get list of methods
            Method[] methods = classobj.getMethods();
  
            // get no of parameters for each
            // method in list of methods
            for (Method method : methods) {
  
                // print name of method
                System.out.print("Method Name is "
                                 + method.getName());
  
                // get no of parameters for each method
                int noOfParameters = method.getParameterCount();
  
                // print no of parameters
                System.out.println(" and No of parameters = "
                                   + noOfParameters);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
// Demo class to apply getParameterCount() method
class DemoClass {
  
    // method name DemoMethod1
    // No of parameters is 1 for this method
    public void DemoMethod1(String parameter1)
    {
        System.out.println("DemoMethod1");
    }
  
    // method name DemoMethod2
    // No of parameters is 2 for this method
    public void DemoMethod2(String parameter1,
                            String parameter2)
    {
        System.out.println("DemoMethod2");
    }
  
    // method name DemoMethod2
    // No of parameters is Zero for this method
    public void DemoMethod3()
    {
        System.out.println("DemoMethod3");
    }
}

chevron_right


Output:

Method Name is DemoMethod1 and No of parameters = 1
Method Name is DemoMethod2 and No of parameters = 2
Method Name is DemoMethod3 and No of parameters = 0
Method Name is wait and No of parameters = 2
Method Name is wait and No of parameters = 1
Method Name is wait and No of parameters = 0
Method Name is equals and No of parameters = 1
Method Name is toString and No of parameters = 0
Method Name is hashCode and No of parameters = 0
Method Name is getClass and No of parameters = 0
Method Name is notify and No of parameters = 0
Method Name is notifyAll and No of parameters = 0

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#getParameterCount–



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.