1
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.

3
  • Please post the error Commented Mar 6, 2014 at 21:05
  • 12
    It's an ambiguous name. Both those classes have a static field named MAX_VALUE. The compiler can't determine which one you want. Commented Mar 6, 2014 at 21:05
  • Also try removing the * and . Commented Mar 6, 2014 at 21:06

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

with regards to your side note - I've programmed in .Net for the past 15 years and am going through Java to gain experience with it - but are you saying that a Java IDE doesn't exist that has the "go to definition" feature that's in Visual Studio for the past 10 years.
4

You're importing MAX_VALUE twice.

It's included both on java.lang.Integer.*; and java.lang.Long.*;

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.