I have seen it first time ...
in a method signature.
I tried to access a .class file. It has a method defined as below
public void addGraphData(GraphData... _graphData) {
}
And that GraphData is nothing but POJO with getters and setters. Why is the .class file displaying GraphData... _graphData
instead of GraphData _graphData
??
The
...
indicates a variable length parameter list.The feature is called Varargs
It allows you to supply a random number of arguments to a method.
It's varargs and can only be used last in a parameter list. The last param can hold more than one object.
See how "a" and "b" has transformed into an array.
That is the varargs syntax: http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
It's treated like a GraphData[] which can be build on the fly as extensible parameters. Arrays.asList() is another such example.
This notation synonym for
public void addGraphData(GraphData[] _graphData) {
}
...
indicatesvarargs
in Java. The[vararg]
attribute specifies that the method takes a variable number of parameters. To accomplish this, the last parameter must be a safe array of VARIANT type that contains all the remaining parameters :The varargs syntax basically lets you specify that there are possible parameters, right? They can be there, or cannot be there. That's the purpose of the three dots. When you call the method, you can call it with or without those parameters. This was done to avoid having to pass arrays to the methods.
Have a look at this:
See When do you use varargs in Java?
It's for this reason,
varargs
is basically not recommended in overloading of methods.System.out.printf();
is an example ofvarargs
and defined as follows.