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:
import java.lang.reflect.Constructor;
public class GFG {
public static void main(String[] args)
{
Class classObj = String. class ;
Constructor[] cons = classObj.getConstructors();
int params = cons[ 0 ].getParameterCount();
System.out.println( "No of Parameters: " + params);
}
}
|
Output:
No of Parameters: 3
Program 2:
import java.lang.reflect.Constructor;
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
Class classObj = ArrayList. class ;
Constructor[] cons = classObj.getConstructors();
for ( int i = 0 ; i < cons.length; i++) {
int params = cons[i].getParameterCount();
System.out.println(
cons[i].toGenericString()
+ "-> parameter count = " + params);
}
}
}
|
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()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Oct, 2019
Like Article
Save Article