Aamir Aamir - 4 months ago 54
Java Question

Static Imports in Java

import static java.lang.Integer.*;
import static java.lang.Long.*;

public class StaticImortError {

public static void main(String args []) {


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.


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?"