Celsius' Notes
Hire me as a freelancer for your mobile (iOS native, cross-platform with React Native), web and backend software needs.
Go to portfolio/resumé
1 min read

Static imports in Java

When working with static methods in Java such as those in the java.lang.Math class it can become quite cumbersome to keep writing the class name over and over again. Java offers static imports to relieve you of the stress of writing more than you have to.
Statically importing class methods and fields allows you to invoke those methods and access those fields without having to write the classname first.
For instance writing: import static java.lang.Math*;
will import all static members of the Math class into your current namespace. Instead of writing Math.cos() to access the cosine method you can now simply write cos(). Same goes for accessing constants such as Math.PI which can now be accessed more succinctly via PI;

Use Sparingly

The Javadocs urge you to use this feature very sparingly. Why? Because it can quickly make your code unmaintainable and hard to read. This is because it makes it harder to know which classes static methods and fields used in your code come from. In particular, you should not import all the static members of a class, as seen in the above example, but rather opt to only statically import those that are used frequently in your code.