I am doing a static import of static members of class Long and Integer as shown below :
Now if i am trying to use this variable MAX_VALUE and print it , i will get an error as shown below :
This is fine. To remove the error i will have to remove one static import to resolve this ambiguity .
The main issue i am getting is, if i use wildcard * with Integer class static import, the class gets compiled with no errors as shown below .
The ambiguity must still exist . Why does this compile with no issues ?
Note : If i use wildcard in Long static import instead of Integer, the code still compiles with no error . The output with wildcard with Integer static import is Long.MAX_VALUE and in other case it is Integer.MAX_VALUE.
Nishant Sinngh wrote:. . . Why does it compile inspite of the ambiguity ?
I tried going through the Java® Language Specification and seeing whether it says anything about single static imports taking precedence over on‑demand static imports. Didn't find anything. Try this JLS section and see whether there is anything helpful there.
Aaaaaaaaaaaaaaaaaah! Is there a JLS section telling us that? Is it the 2nd bullet under §6.5.2:-
Stephan van Hulst wrote:Specific imports always take precedence over wildcard imports. . . .
Does it explicity say that single imports take rpecedence over on‑demand imports?
JLS .5.2 wrote:• Otherwise, if a field of that name is declared in the compilation unit (§7.3) containing the Identifier by a single-static-import declaration (§7.5.3), or by a static-import-on-demand declaration (§7.5.4) then the AmbiguousName is reclassified as an ExpressionName.
The Java® Language Specification Java SE 8 Edition wrote:
A single-type-import declaration d in a compilation unit c of package p that imports a type named n shadows, throughout c, the declarations of:
any top level type named n declared in another compilation unit of p any type named n imported by a type-import-on-demand declaration in c any type named n imported by a static-import-on-demand declaration in c