Constructor getParameterCount() method in Java with Examples

The getParameterCount() method of java.lang.reflect.Constructor class is used to return the number of parameters present on this constructor object. Every constructor contains a number of parameters starting from zero to many. This method is helpful to get those numbers of parameters.

Syntax:

public int getParameterCount()

Parameters: This method accepts nothing.

Return: This method returns the number of formal parameters for the executable this object represents.

Below programs illustrate getParameterCount() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate getParameterCount() method
  
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = String.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        // get count of parameter
        int params = cons[0].getParameterCount();
  
        // print parameter count
        System.out.println("No of Parameters: " + params);
    }
}

chevron_right


Output:

No of Parameters: 3

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate getParameterCount() method
  
import java.lang.reflect.Constructor;
import java.util.ArrayList;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = ArrayList.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        for (int i = 0; i < cons.length; i++) {
            // get count of parameter
            int params = cons[i].getParameterCount();
  
            // print parameter count
            System.out.println(
                cons[i].toGenericString()
                + "-> parameter count = " + params);
        }
    }
}

chevron_right


Output:

public java.util.ArrayList(java.util.Collection)-> parameter count = 1
public java.util.ArrayList()-> parameter count = 0
public java.util.ArrayList(int)-> parameter count = 1

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getParameterCount()

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




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.