Reputation: 2424
import static java.lang.Integer.*;
import static java.lang.Long.*;
public class StaticImortError {
public static void main(String args []) {
System.out.println(MAX_VALUE);
}
}
Can anybody please explain why this program is showing compile time error, if I tried to use imports like
import static java.lang.Integer.*;
import static java.lang.Long.MAX_VALUE;
, it ran fine and as expected displayed the maximum value of long data types, but with the above imports its showing error.
Upvotes: 1
Views: 3161
Reputation: 30528
The problem is that you must explicitly state what to import in this case since both classes have a MAX_VALUE
constant.
If you open the source code you'll see.
Since you can't assign an alias in java you are stuck with using Integer.MAX_VALUE
/Long.MAX_VALUE
.
Just a side note: I do not suggest a static
import for Integer.MAX_VALUE
(nor Long.MAX_VALUE
) because if you have a rather big class and in the middle you reference MAX_VALUE
then someone in the future will scratch his head asking "Whose max value are we talking about?"
Upvotes: 7
Reputation: 10707
You're importing MAX_VALUE
twice.
It's included both on java.lang.Integer.*;
and java.lang.Long.*;
Upvotes: 4
Reputation: 476493
In that case you import the static methods and fields associated with the given class.
For instance the Assert
class contains a lot of methods like assertEquals
.
Instead of writing each time Assert.assertEquals
, you can write
import static org.junit.Assert.*;
and then use assertEquals
in your code.
The reason why this doesn't work is because a call to MAX_VALUE
is ambiguous between Long.MAX_VALUE
and Integer.MAX_VALUE
Upvotes: 0