Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday 27 July 2017

Static Import In Java

Java Static Import

Static Import In Java

The java static import feature is introduced in Java 5 and by using static import feature of java 5, the java programmer can access any static member i.e static field or a static method of a class directly. And there is no need to class name to call any method or field of a class.


When to Use Static Imports?

Now there is a question that when to use static import? Suppose you are going to use static variables or methods a lot so for you, it will be better
to use the static import. For example, if you are going to write a code for mathematical calculation then here you should use static import in java.

Advantage of Static Import In Java

  1. The first advantage of java static import is that it reduces the code length.
  2. The static import saves time because it reduces the length of the code. 


Drawbacks of Static Import In Java

  • The static import feature reduces the readability of java program.
  • If you use java static import much time, it makes a program unreadable and unmaintainable.
Let's understand with examples, I am sure you will be better understood with an example.

Java Simple Example - Without Static Import

public class Simple
{
public static void main(String args[])
{
double d1 = Math.sqrt(6.0);//Using class name Math
double d2 = Math.tan(50);//Using class name Math
System.out.println("Square of 6 is : "+d1);
System.out.println("Tan of 50 is : "+d2);
}
}

Output: Square of 6 is : 2.449489742783178
              Tan of 50 is : -0.27190061199763077

In the above example, There we did not use any static import therefore we have to use the class name as I mention above e.g Math.




Java Static Import Example

In this example, we will import Math class i.e static import.

import static java.lang.Math.*;
public class Demo
{
public static void main(String args[])
{
double d1 = sqrt(6.0);//Using static method without Math class
double d2 = tan(50);//Using static method without Math class
System.out.println("Square of 6 is : "+d1);
System.out.println("Tan of 50 is : "+d2);
}
}

Output: Square of 6 is : 2.449489742783178
              Tan of 50 is : -0.27190061199763077

In the above example, here we import i.e static import of a Math, therefore, we did not use Math class here.


Java Static Import Example 1

This is another example of java static import, here in this example we will import static import of a System class.

import static java.lang.System.*;
public class SystemExample
{
public static void main(String args[])
{
out.println("Hello java ");//There is no need of System class
out.println("I love java ");
out.println("Wow javaaaa ");
}
}

Output: Hello java 
              I love java 
              Wow javaaaa

In the above example, We did not use System class in System.out.println() statement because of static import of System class. So there is no need to use System class to call any method or variable.

Now we have learned so many things about "static" like static keyword in java(static variable, static method and static class), static import in java, etc. 

Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate