Given how a method with var args is used any other format could lead to ambiguities. Having the varargs last prevents possible ambiguities without requiring additional syntax to resolve the ambiguities which would lessen the benefit of the feature.
Consider the follow method declaration:
public void varargsAreCool(String surname, String firstname,
String... nicknames) {
// some cool varargs logic
}
When used like varargsAreCool("John", "Smith")
it is obvious that John Smith
has no nicknames. When used like this varargsAreCool("Andrew", "Jones", "The Drew", "Jonesy")
.
Now consider the following invalid method declaration:
public void varargsAreCool(String surname, String... nicknames,
String firstname) {
// some cool varargs logic
}
When used like varargsAreCool("John", "Smith")
is Smith
John's nickname or his surname? If it is his surname how do I indicate that he has no nicknames? To do this you would probably have to use the method like this varargsAreCool("John", new String[]{}, "Smith")
which is clunky and somewhat defeats the purpose of the feature.
When used like this varargsAreCool("Andrew", "The Drew", "Jonesy", "Jones")
are the The Drew, Jonesy and Jones
all nicknames and the surname is missing? Again this ambiguity could be resolved but at the cost of clunky additional syntax.