double r = Math.cos(Math.PI * theta);
or
System.out.println("Blah blah blah");
Code language: Java (java)
You may want to avoid unnecessary use of static class members like Math. and System. For this use static import. For example above code when changed using static import is changed to: import static java.lang.System.out;
import static java.lang.Math.PI;
import static java.lang.Math.cos;
...
double r = cos(PI * theta);
out.println("Blah blah blah");
...
Code language: Java (java)
So whats the advantage of using above technique? Only advantage that I see is readability of the code. Instead of writing name of static class, one can directly write the method or member variable name. Also keep one thing in mind here. Ambiguous static import is not allowed. i.e. if you have imported java.lang.Math.PI and you want to import mypackage.Someclass.PI, the compiler will throw an error. Thus you can import only one member PI. Happy importing.. :) Further Reading: Varargs in Java Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Why bother importing Math.PI and Math.cos if you don't save anything in the code ( Math.cos(Math.PI * theta) ?
Rightly pointed, Some cases I prefer to prefix name of Class but sometime it unnecessary bloat the code e.g.consider calling methods like assertNull from Junit as Assert.assertNull(), nothing wrong but I still like former one. Some more usecase discussed on where to use static import in Java
Thanks for pointing out the error. I have changed the code. (Copy/Paste effect ;-))
Looks like a fun little feature.
I agree, but if in single class there are too many static imports, it will be real difficult to know which function belongs to which class. This will make readability bit diificult.
What are the benfits of static import. :)
good. keep it up
cool. -:D
Nice post