3

When is it appropriate to perform a static import package.package.class ? Nothing seems to answer this question on Google.

A static import allows you to write this:

out.print("The result is "); rather than this:

System.out.print("The result is ");

This does not make sense to me. If I import that class why will its methods also not be imported?

4 Answers 4

5

Using static imports allows you to turn this:

double r = Math.cos(Math.PI * theta);

into this:

double r = cos(PI * theta);

The static imports tells the class where these methods are coming from so you do not need to preceed the method with the class name when using static imports

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

Comments

1

Try import static java.lang.System.out;. You aren't importing its' method, you're importing its' static out member variable.

Comments

0

static import is a convenience. If you are repeatedly using static members of some class in your code, a static import will save you from writing as much.

System.out.println becoming out.println is probably one of the most common uses, due to its connection with console debugging and console applications in general. However, any class with static members (methods or fields) can be effectively used. If you make extensive use of Math, for example, statically importing that class would make your calls to your mathematics functions simpler. If you frequently make calls to methods of a singleton class, statically importing that class would let you go from Singleton.INSTANCE.method() to INSTANCE.method().

As the static import is purely for convenience's sake, it may make more sense to ask when it's inappropriate to use, rather than when to use it. You shouldn't use static import when:

  • You will create name collisions between multiple static imports (I believe javac will yell at you about collisions anyway, though)
  • It will be unclear what class the static members you're using are coming from; out.println is familiar enough to a Java developer that they'll assume it's from System immediately... so don't statically import some other field named "out" which has a method "println"! cos, sin, tan, pow, and friends can be pretty obviously deduced to be from Math, especially in context.

In general, I wouldn't recommend statically importing from more than one class per file, simply to reduce confusion when reading code. "Is foo(Bar b) coming from the import of Fizz or Buzz? Or wait, is it being imported from somewhere else at all?!" is not the sort of question you want a future developer to ask themselves; and remember that it won't take long for you to forget all the design decisions you made for your own code, so be nice to yourself as well as others!

2 Comments

You stated: "so don't statically import some other field named "out" which has a method "println"!" How can a field have a method? I always thought a class member was one or the other?
@IqbalHamid, In System.out.println, out is a static field of the System class. In this case, out is an instance of the PrintStream class, and therefore has methods of its own. Any field which is a class instance could have methods.
0

You can use it anywhere however it might make code less readable. However there are exceptions, eg it is generally accepted practice to use static imports in JUnit tests

import static org.junit.Assert;

...

assertEquals(expected, actual);

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.