静态导入是Java版本5及更高版本中引入的功能。这意味着,如果将类中的字段和方法定义为公共静态,则无需指定其类即可在代码中使用它们。
给出了一个用Java演示此程序的程序,如下所示:
import static java.lang.System.*;
public class Demo {
   public static void main(String args[]) {
      out.println("Demonstration of Static Import in Java");
      int a = 10;
      out.println("Value of a is: " + a);
   }
}输出结果
Demonstration of Static Import in Java Value of a is: 10
现在让我们了解上面的程序。
System.out.println()中不需要System类,因为java.lang包使用了静态导入。因此,无需将System类指定为使用System类的out成员字段和println方法。演示此代码段如下:
out.println("Demonstration of Static Import in Java");
int a = 10;
out.println("Value of a is: " + a);