Using Static Import in Java

Share on Google+0Tweet about this on Twitter0Share on Facebook0Share on LinkedIn0

To import a specific member inside your java source file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. Here’s how you would import the Color class to Rectangle class.

package com.java.test;

public class Constants {

	public static final double PI = 3.141592653589793;
}

Importing Constants class

import com.java.test.Constants;

public class Circle {
	private double getArea(int radious) {
		return Constants.PI * radious * radious;

	}

	public static void main(String[] args) {
		Circle myCircle = new Circle();
		System.out.println("Area=" + myCircle.getArea(20));
	}

}

This approach works well. However, Java5 has enhanced import feature called static import. Static import can be used when you want to use classe’s static members. After using static import in the above example

import static com.java.test.Constants.PI;

public class Circle {
	private double getArea(int radious) {
		return PI * radious * radious;

	}

	public static void main(String[] args) {
		Circle myCircle = new Circle();
		System.out.println("Area=" + myCircle.getArea(20));
	}

}

However, both classes produce the same output.

Note:

  1. To use static import we have to use fully qualified name of the static member you want to import, or a wildcard. In our example we are importing PI from com.java.test.Constants class
  2. In case we have to import several static members, then we can use wildcard (*), which will import all of the static members of that class.
  3. After static import we don’t have to write the class name while calling. You can see the example in case-1 where we are using Constants.PI but after static import we just use member name PI.
  4. You must use import static <packagename.Classname.MemberName>
  5. Static import and ambiguity: If your are importing the static member of two classes that shares common names for two or more members, which leads to an ambiguity. Java compiler wont understand which member it is referring to.

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. Follow him on Twitter or Google plus.

-- Advertisement --

Leave a Reply