Java “import”后面的“static”修饰符是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/162187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:56:26  来源:igfitidea点击:

What does the "static" modifier after "import" mean?

javamodifierstatic-import

提问by JSON

When used like this:

像这样使用时:

import static com.showboy.Myclass;

public class Anotherclass{}

what's the difference between import static com.showboy.Myclassand import com.showboy.Myclass?

什么之间的区别import static com.showboy.Myclassimport com.showboy.Myclass

采纳答案by xsl

See Documentation

查看文档

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

静态导入声明类似于普通的导入声明。正常的导入声明从包中导入类,允许它们在没有包限定的情况下使用,静态导入声明从类中导入静态成员,允许它们在没有类限定的情况下使用。

那么什么时候应该使用静态导入呢?非常节俭!仅当您试图声明常量的本地副本或滥用继承(常量接口反模式)时才使用它。换句话说,当您需要频繁访问一两个类的静态成员时使用它。如果您过度使用静态导入功能,它会使您的程序不可读和不可维护,您导入的所有静态成员都会污染其命名空间。你的代码的读者(包括你,在你编写它几个月后)不会知道静态成员来自哪个类。从类中导入所有静态成员对可读性特别有害;如果您只需要一两个成员,请单独导入它们。使用得当,静态导入可以让你的程序更具可读性,

回答by Nicolas

Static import is used to import static fields / method of a class instead of:

静态导入用于导入类的静态字段/方法,而不是:

package test;

import org.example.Foo;

class A {

 B b = Foo.B_INSTANCE;

}

You can write :

你可以写 :

package test;

import static org.example.Foo.B_INSTANCE;

class A {

 B b = B_INSTANCE;

}

It is useful if you are often used a constant from another class in your code and if the static import is not ambiguous.

如果您经常在代码中使用来自另一个类的常量,并且静态导入没有歧义,这将很有用。

Btw, in your example "import static org.example.Myclass;" won't work : import is for class, import static is for static members of a class.

顺便说一句,在您的示例中“导入静态 org.example.Myclass;” 不起作用:import 是针对类的,import static 是针对类的静态成员的。

回答by Victor

There is no difference between those two imports you state. You can, however, use the static import to allow unqualified access to static members of other classes. Where I used to have to do this:

您声明的这两个进口之间没有区别。但是,您可以使用静态导入来允许对其他类的静态成员进行非限定访问。我曾经必须这样做的地方:

import org.apache.commons.lang.StringUtils;
      .
      .
      .
if (StringUtils.isBlank(aString)) {
      .
      .
      .

I can do this:

我可以做这个:

import static org.apache.commons.lang.StringUtils.isBlank;
      .
      .
      .
if (isBlank(aString)) {
      .
      .
      .

You can see more in the documentation.

您可以在文档中查看更多信息

回答by user85421

the difference between "import static com.showboy.Myclass" and "import com.showboy.Myclass"?

“import static com.showboy.Myclass”和“import com.showboy.Myclass”的区别?

The first should generate a compiler error since the static import only works for importing fields or member types. (assuming MyClass is not an inner class or member from showboy)

第一个应该生成编译器错误,因为静态导入仅适用于导入字段或成员类型。(假设 MyClass 不是 showboy 的内部班级或成员)

I think you meant

我想你的意思是

import static com.showboy.MyClass.*;

which makes all static fields and members from MyClass available in the actual compilation unit without having to qualify them... as explained above

这使得 MyClass 中的所有静态字段和成员都可以在实际编译单元中使用,而无需对其进行限定......如上所述

回答by Rahul Saxena

The basic idea of static import is that whenever you are using a static class,a static variable or an enum,you can import them and save yourself from some typing.

静态导入的基本思想是,无论何时您使用静态类、静态变量或枚举,您都可以导入它们并避免一些输入。

I will elaborate my point with example.

我将举例说明我的观点。

import java.lang.Math;

class WithoutStaticImports {

 public static void main(String [] args) {
  System.out.println("round " + Math.round(1032.897));
  System.out.println("min " + Math.min(60,102));
 }
}

Same code, with static imports:

相同的代码,使用静态导入:

import static java.lang.System.out;
import static java.lang.Math.*;

class WithStaticImports {
  public static void main(String [] args) {
    out.println("round " + round(1032.897));
    out.println("min " + min(60,102));
  }
}

Note: static import can make your code confusing to read.

注意:静态导入会使您的代码难以阅读。

回答by Java Main

Say you have static fields and methods inside a class called MyClassinside a package called myPackageand you want to access them directly by typing myStaticFieldor myStaticMethodwithout typing each time MyClass.myStaticFieldor MyClass.myStaticMethod.

假设您在名为MyClass的包内调用的类中有静态字段和方法,myPackage并且您希望通过每次键入myStaticFieldmyStaticMethod不键入来直接访问它们 MyClass.myStaticFieldMyClass.myStaticMethod

Note : you need to do an import myPackage.MyClassor myPackage.*for accessing the other resources

注意:您需要执行 import myPackage.MyClassmyPackage.*访问其他资源

回答by roottraveller

The importallows the java programmer to access classes of a package without package qualification.

import允许Java程序员包的接入类,而包的资格。

The static importfeature allows to access the static members of a class without the class qualification.

static import功能允许在没有类限定的情况下访问类的静态成员。

The importprovides accessibility to classes and interface whereas static importprovides accessibility to static members of the class.

所述import的类提供可访问性和接口而static import提供可访问到类的静态成员。

Example :

例子 :

With import

进口

import java.lang.System.*;    
class StaticImportExample{  
    public static void main(String args[]){  

       System.out.println("Hello");
       System.out.println("Java");  

  }   
} 

With static import

静态导入

import static java.lang.System.*;    
class StaticImportExample{  
  public static void main(String args[]){  

   out.println("Hello");//Now no need of System.out  
   out.println("Java");  

 }   
} 

See also : What is static import in Java 5

另请参阅:Java 5 中的静态导入是什么

回答by RajeeV VenkaT

The staticmodifier after importis for retrieving/using static fields of a class. One area in which I use import staticis for retrieving constants from a class. We can also apply import staticon static methods. Make sure to type import staticbecause static importis wrong.

static后面的修饰符import用于检索/使用类的静态字段。我使用的一个领域import static是从类中检索常量。我们也可以应用于import static静态方法。确保输入import static因为static import是错误的。

What is static importin Java - JavaRevisited- A very good resource to know more about import static.

什么是static importJava中- JavaRevisited-一个很好的资源更多的了解import static