OK, so there's something that bothers me a little in the Java syntax that I don't quite understand.
When an annotation accepts an array as one of its argument's value, we can do so by providing the values inside a couple of brackets, like so:
public @interface SomeAnnotation {
String[] someParam();
}
@SomeAnnotation(someParam = { "foo", "bar" })
public class Example {}
This is a very neat and concise way of passing an array of values. Love it.
But the same can not be done when calling a method:
public interface Example {
public static void someMethod(String[] someParam) {
// Do something
}
}
// ...
Example.someMethod({ "foo", "bar" }); // Syntax Error :(
From the look of it, both the annotation's someParam
and the method someParam
are declared as a String[]
array.
I suppose its related to the way annotation are handled by Java under the hood, and/or the fact that someParam
is declared like a kind of method (with parenthesis at the end) in SomeAnnotation
, but still... why?
If someone out there could shed some light on this, I would be very grateful.
Please note that I'm not looking for alternatives or workaround, but rather some explanations.
Subsequent question
I know I could call the method with the new String[]...
syntax, like so:
Example.someMethod(new String[] { "foo", "bar" });
But that also seems strange to me since the param is supposed to be an array of String
and the values inside the brackets are indeed String
s. I would expect the compiler to be able to match things up and be OK with that.