Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Lets say we have these packages and classes:

package p1;

public class A1 {
    public static void a() {}
}

package p2;

public class A1 {
    public static void a() {}
}

package p3;

import static p1.A1.a;
import static p2.A1.a;

public class A1 {
    public static void test() {

    }
}

I am wondering, why the static import of methods is legal (won't result in compile time error) in package p3? We won't be able to use them further in the test() method as such usage will result in the compile time error.

Why it is not the same as with a normal import of classes. Lets say we would like to import classes A1 from packages p1 and p2 into p3:

package p3;
import p1.A1;
import p2.A1;

such import is illegal and will result in the compile time error.

like image 265
Janek Avatar asked Oct 22 '12 10:10

Janek


People also ask

Why should we avoid static imports?

Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

Can you import a static method?

With the help of import, we are able to access classes and interfaces which are present in any package. But using static import, we can access all the static members (variables and methods) of a class directly without explicitly calling class name.

Why are static imports bad Java?

by Joshua Bloch.) This is considered bad Java programming practice because when a class implements an interface, it becomes part of the class's public API. Implementation details, such as using the static members of another class, should not leak into public APIs.

What is the use of static imports?

Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static , to be used in Java code without specifying the class in which the field has been defined.


2 Answers

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.

like image 50
Joachim Sauer Avatar answered Oct 03 '22 12:10

Joachim Sauer


It is because you have named all of these classes the same thing. Every time you call this static method it is looking in the most local class which in this case is the A1 in p3 which does not contain the static method a(). Always remember a class name should be unique and never the same as another.

like image 44
ExoNaut Avatar answered Oct 03 '22 11:10

ExoNaut