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.
I found this to be very convenient when using Utility classes.
For example , instead of using :
if(CollectionUtils.isNotEmpty(col))
I can instead :
Which IMO increases code readability when I use this utility multiple times in my code.
I use it for Color a lot.
It is very unlikely that the colors will be confused with something else.
Another reasonable use for static imports is with JUnit 4. In earlier versions of JUnit methods like
assertEquals
andfail
were inherited since the test class extendedjunit.framework.TestCase
.In JUnit 4, test classes no longer need to extend
TestCase
and can instead use annotations. You can then statically import the assert methods fromorg.junit.Assert
:JUnit documents using it this way.
I recommend the use of static import when using OpenGL with Java, which is a use-case falling into the "heavy use of constants from a utility class" category
Consider that
allows you to port original C code and write something readable such as :
instead of that common widespread ugliness :
I agree that they can be problematic from a readability perspective and should be used sparingly. But when using a common static method they can actually increase readability. For example, in a JUnit test class, methods like
assertEquals
are obvious where they come from. Similarly for methods fromjava.lang.Math
.I think static import is really useful to remove redundant class names when using utils classes like
Arrays
andAssertions
.Not sure why but Ross skipped out the last sentence that mentions this in the documentation he is referencing.
Basically copied from this blog: https://medium.com/alphadev-thoughts/static-imports-are-great-but-underused-e805ba9b279f
So for example:
Assertions in tests
This is the most obvious case which I think we all agree on
Utils classes and enums
The class name can be removed in many cases when using utils classes making the code easier to read
java.time package has a few cases where it should be used
Example of when not to use