Tuesday, 5 May 2015

static import in Java

In order to access any static member (static field or method) of the class, it is necessary to qualify references with the class they came from.
ClassName.static_method()

With static import feature of Java 5, members defined in a class as public static can be used without qualifying it with the class name, in any Java class which does a static import. This shortens the syntax required to use a static member.

Syntax of static import

Any Java program can import the members individually or en masse-

// accessing specific static variable of a class
import static package_name.class_name.static_variable;

// accessing specific static method of a class
import static package_name.class_name.static_method;

// accessing all the static members of a class en masse
import static package_name.class_name.*;

static import Example

public class MyClass {
 public static final int COUNT = 1;
 public static int incrementCounter(int num){
  return num + COUNT;
 }
}
import static org.netjs.examples.MyClass.*;
public class StaticImportDemo {
 public static void main(String[] args) {
  // Accessing the static field of MyClass with out 
  //qualifying it with class name
  System.out.println("Count field  of MyClass " + COUNT);
  // invoking the static method of MyClass with out 
  //qualifying it with class name
  System.out.println("Calling static method of MyClass " + incrementCounter(5));
 }
}

It can be seen that static import is done here to get all the static members of the MyClass,

import static org.netjs.examples.MyClass.*;

After that the static field and methods of MyClass can be used with out qualifying them with Class.

Benefits of static import

  • when you require frequent access to static members from one or two classes it is a good idea to use static import in order to avoid qualifying with class name every time.
  • To avoid constant interface antipattern.
    Read about it here and here

Disadvantages of static import

  • Overuse of static import feature may result in unreadable and unmaintainable program. As it will be very difficult to recognize which class the particular static member comes from.
  • It may result in error if same static member is imported from 2 different classes. As compiler will not be able to determine which one to use in absence of class name qualification.
  • As Exp
    static import in java

    Here, while trying to access MAX_VALUE field compiler is giving "The field MAX_VALUE is ambiguous" error as field MAX_VALUE is in both INTEGER AND LONG classes. By doing a static import of all the members of both these classes we brought those members into the global namespace, thus the name collision.

Points to note -

  • 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 it can increase readability of a program by removing the boilerplate of repetition of class names.
  • Overuse may result in unreadable code as it will be difficult to recognize from which class the particular static member comes from.
  • There may be a name collision error in case 2 classes have the same name static variable and both of them are imported individually.

That's all for this topic static import in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. static in Java
  2. static method overloading or overriding in Java
  3. Why main method static in Java
  4. static reference to the non-static method or field error
  5. interface static methods in Java 8
  6. static block in Java

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment