Why are static imports of static methods with same names legal?

The ambiguity of the static imports of methods could be resolved at the point of the method invocation.

For example if you had a static import for two methods that look like this:

void frobnicate(int i);
// and
void frobnicate(boolean b);

Then you could import and use both, because the compiler could tell which one to use, based on the arguments you pass in (frobnicate(1) calls the first one, frobnicate(true) calls the second one).

With classes, that’s not possible: Foobar a; alone is not sufficient to tell you which of the two Foobar classes you want.

Also note that a single static import can import multiple names. According to the relevant section of the JLS (emphasis mine):

A single-static-import declaration imports all accessible static members with a given simple name from a type.

For example if the two frobnicate methods above where located in the same class, a single static import could import them both.

Leave a Comment

tech