How important it is to convert all my import to static import
? Why are people still reluctant to use static import
?
So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes.
by Joshua Bloch.) This is considered bad Java programming practice because when a class implements an interface, it becomes part of the class's public API. Implementation details, such as using the static members of another class, should not leak into public APIs.
Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static , to be used in Java code without specifying the class in which the field has been defined.
import static is used to import static members of a class, you can import a specific member of class or import en masse. Used sparingly and in case of importing static members of one or two classes, static import in Java can increase readability of a program by removing the boilerplate of repetition of class names.
As the docs say, use it sparingly. Look there for the justifications.
This is a special case but also the perfect use case (and I use it in all my tests):
import static junit.framework.Assert.*;
Here, I find that this makes my tests more readable and it's obvious from where assertXXX
come from. But this is an exception. In other situations, I find that static import make things more obscure, harder to read and I don't really use them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With