This
is used when we don’t know how much argument should be passed to the Method but
this is not a good practice in real time environment as it leads to maintenance
problems. TO understand this more clearly please see the example below.
public class demoVarArgs
{
void demoMethod(int a)
{
System.out.println("One argument demoMethod is called");
}
void demoMethod(int a,int b)
{
System.out.println("Two argument demoMethod is called");
}
void demoMethod(int...a)
{
System.out.println("Varible argument demoMethod is called");
}
public static void main(String
s[])
{
demoVarArgs obj = new
demoVarArgs();
obj.demoMethod(1);
obj.demoMethod(1,2);
obj.demoMethod(1,2,3,4,5,6,7,8,9,10);
}
}
Output:
One argument demoMethod is called
Two argument demoMethod is called
Varible argument demoMethod is called
In the above example there are three demoMethods
with one ,two and variable number of arguments and we called the demoMethod by
giving one, two and variable number of arguments. When it meets the exact
number of argument defined in the method it called that particular method
otherwise it always call method with variable number of arguments.
No comments:
Post a Comment