If I have the method like public int sum(int a, int b, int c) { }
Answer: Parameters count is 3
Methods having parameters has not fixed, sometimes it might be 2 or 1 or 4 How to find out this through Java code ?
If I have the method like public int sum(int a, int b, int c) { }
Answer: Parameters count is 3
Methods having parameters has not fixed, sometimes it might be 2 or 1 or 4 How to find out this through Java code ?
You can use optional param.
private static void sum(int... params) {
System.out.println(params.length);
//some logic
}
Use this method whatever you want. Forexample ;
sum(1,2,3); // it returns 3
sum(1,2); // it returns 2
sum(1,2,29,323,123); // it returns 5
sum(1,2,3,4,5,6,7,8); // it returns 8
In sum
method , your parameters are set in int array
. Also you can get like
params[0] // this refers first param
params[1] // this refers second param
//and others.
Please be careful on arraylength
when getting parameter. Dont get exception.