Just got a review comment that my static import of the method was not a good idea. The static import was of a method from a DA class, which has mostly static methods. So in middle of the business logic I had a da activity that apparently seemed to belong to the current class:
import static some.package.DA.*;
class BusinessObject {
void someMethod() {
....
save(this);
}
}
The reviewer was not keen that I change the code and I didn't but I do kind of agree with him. One reason given for not static-importing was it was confusing where the method was defined, it wasn't in the current class and not in any superclass so it too some time to identify its definition (the web based review system does not have clickable links like IDE :-) I don't really think this matters, static-imports are still quite new and soon we will all get used to locating them.
But the other reason, the one I agree with, is that an unqualified method call seems to belong to current object and should not jump contexts. But if it really did belong, it would make sense to extend that super class.
So, when does it make sense to static import methods? When have you done it? Did/do you like the way the unqualified calls look?
EDIT: The popular opinion seems to be that static-import methods if nobody is going to confuse them as methods of the current class. For example methods from java.lang.Math and java.awt.Color. But if abs and getAlpha are not ambiguous I don't see why readEmployee is. As in lot of programming choices, I think this too is a personal preference thing.
Thanks for your response guys, I am closing the question.
This is from Sun's guide when they released the feature (emphasis in original):
(https://docs.oracle.com/javase/8/docs/technotes/guides/language/static-import.html)
There are two parts I want to call out specifically:
extend some.package.DA
? If so, static imports may be a cleaner way of handling this. If you never would have dreamed of extendingsome.package.DA
, then this is probably a poor use of static imports. Don't use it just to save a few characters when typing.import static some.package.DA.save
instead ofDA.*
. That will make it much easier to find where this imported method is coming from.Personally, I have used this language feature very rarely, and almost always only with constants or enums, never with methods. The trade-off, for me, is almost never worth it.
They're useful to reduce verbiage, particularly in cases where there are a lot of imported methods being called, and the distinction between local and imported methods is clear.
One example: code that involves multiple references to java.lang.Math
Another: An XML builder class where prepending the classname to every reference would hide the structure being built
I use them when ever I can. I have IntelliJ setup to remind me if I forget. I think it looks much cleaner than a fully qualified package name.
Effective Java, Second Edition, at the end of Item 19 notes that you can use static imports if you find yourself heavily using constants from a utility class. I think this principle would apply to static imports of both constants and methods.
This has advantages and disadvantages. It makes the code a bit more readable at the expense of losing some immediate information about where the method is defined. However, a good IDE will let you go to the definition, so this isn't much of an issue.
You should still use this sparingly, and only if you find yourself using things from the imported file many, many times.
Edit: Updated to be more specific to methods, as that's what this question is referring to. The principle applies regardless of what's being imported (constants or methods).
IMO static import is quite a nice feature. It is absolutely true that heavy reliance on static import makes the code unreadable and difficult to understand which class a static method or attribute belongs to. However, in my experience it becomes a usable feature especially when designing
Util
classes which provide some static methods and attributes. The ambiguity arising whenever providing static import can be circumvented by establishing code standards. In my experience within a company this approach is acceptable and makes the code cleaner and easy to understand. Preferably I insert the_
character in front static methods and static attributes (somehow adopted from C). Apparently this approach violates the naming standards of Java but it provides clarity to code. For example, if we have a AngleUtils class:In this case the static import provides clarity and code structure looks more elegant to me:
Right away someone can tell the which method or attribute comes from a static import and it hides the information of the class which it belongs to. I dont suggest using static import for classes that are integral part of a module and provide static and non-static methods as in these case it is important to know which class provides certain static functionality.
I think static imports are neat for NLS in the gettext-style.
This both marks the string as a string that has to be extracted and provides an easy and clean way to replace the string with its translation.