- This is about varargs in Java. They are called Variadic arguments.And the function having such arguments is called Variadic Function.
- They are to be used when you need to use unknown number of arguments of same type.
e.g. log function where you need to print a string which can be formed of any number of string objects.
- This argument must be the last parameter of a method.
For examples -
click
This works nice in case of polymorphism.
public class Test
{
public static void fun(Parent... args){
for (Parent i : args){
i.setA(5);
System.out.println("a is " + i.getA());
}
}
public static void main(String args[])
{
Parent p = new Parent();
ChildA a = new ChildA();
ChildB b = new ChildB();
fun(p,a,b);
}
}
No comments:
Post a Comment