简体   繁体   English

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

[英]What does the “static” modifier after “import” mean?

When used like this:像这样使用时:

import static com.showboy.Myclass;

public class Anotherclass{}

what's the difference between import static com.showboy.Myclass and import com.showboy.Myclass ? import static com.showboy.Myclassimport com.showboy.Myclass什么import com.showboy.Myclass

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.如果使用得当,静态导入可以通过删除类名重复的样板文件使您的程序更具可读性。

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 .您可以在文档中查看更多信息

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;"顺便说一句,在您的示例中“导入静态 org.example.Myclass;” won't work : import is for class, import static is for static members of a class.不起作用:import 是针对类的,import static 是针对类的静态成员的。

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.注意:静态导入会使您的代码难以阅读。

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 中的所有静态字段和成员都可以在实际编译单元中使用,而无需对其进行限定......如上所述

The import allows the java programmer to access classes of a package without package qualification. import允许 Java 程序员在没有包限定的情况下访问包的类。

The static import feature allows to access the static members of a class without the class qualification. static import功能允许在没有类限定的情况下访问类的静态成员。

The import provides accessibility to classes and interface whereas static import provides 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 中的静态导入是什么

Say you have static fields and methods inside a class called MyClass inside a package called myPackage and you want to access them directly by typing myStaticField or myStaticMethod without typing each time MyClass.myStaticField or MyClass.myStaticMethod .假设您在名为myPackage的包中名为MyClass的类中有静态字段和方法,并且您希望通过键入myStaticFieldmyStaticMethod直接访问它们,而无需每次都键入MyClass.myStaticFieldMyClass.myStaticMethod

Note : you need to do an import myPackage.MyClass or myPackage.* for accessing the other resources注意:您需要import myPackage.MyClassmyPackage.*以访问其他资源

The static modifier after import is for retrieving/using static fields of a class. import后的static修饰符用于检索/使用类的静态字段。 One area in which I use import static is for retrieving constants from a class.我使用import static一个领域是从类中检索常量。 We can also apply import static on static methods.我们还可以在静态方法上应用import static Make sure to type import static because static import is wrong.确保输入import static因为static import是错误的。

What is static import in Java - JavaRevisited - A very good resource to know more about import static . 什么是 Java 中的static import - JavaRevisited - 一个非常好的资源,可以了解更多关于import static

Very good exaple.很好的例子。 npt tipical with MAth in wwww.... npt 典型与 wwww 中的数学......

https://www.java2novice.com/java-fundamentals/static-import/https://www.java2novice.com/java-fundamentals/static-import/

public class MyStaticMembClass {
 
    public static final int INCREMENT = 2;
     
    public static int incrementNumber(int number){
        return number+INCREMENT;
    }
}

in onother file inlude在另一个文件中

import static com.java2novice.stat.imp.pac1.MyStaticMembClass.*;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM