java static关键字在导入java.lang.System类中的作用是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10756657/
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
What is the role of static keyword in importing java.lang.System class?
提问by Green
I don't understand the meaning of the keyword static
when I import System
class:
我static
在导入System
类时不明白关键字的含义:
import static java.lang.System.*
I'm reading the book about Java and it's written there:
我正在阅读关于 Java 的书,它写在那里:
Any import declaration that doesn't use the word
static
must start with the name of a package and must end with either of the following:
- The name of a class within that package
- An asterisk (indicating all classes within that package)
For example, the declaration import
java.util.Scanner;
is valid becausejava.util
is the name of a package in the Java API, andScanner
is the name of a class in thejava.util
package.Here's another example. The declaration
import javax.swing.*;
is valid becausejavax.swing
is the name of a package in the Java API, and the asterisk refers to all classes in thejavax.swing
package.
任何不使用该词的导入声明都
static
必须以包的名称开头,并且必须以下列任一结尾:
- 该包中的类名
- 星号(表示该包中的所有类)
例如,声明 import
java.util.Scanner;
是有效的,因为java.util
是 Java API 中包Scanner
的名称,并且 是java.util
包中类的名称。这是另一个例子。声明
import javax.swing.*;
是有效的,因为javax.swing
是 Java API 中的包名,星号指的是包中的所有类javax.swing
。
And I have the following code:
我有以下代码:
public class Addition {
public static void main(String[] args) {
double num;
num = 100.53;
num = num + 1000;
// So when I want to import the whole package java.lang as written in the book, it doesn't work:
// import java.lang.*;
// or like this:
// import static java.lang.*;
// NetBeans in both cases doesn't see these abbreviated names `out` and throws errors. Why?
out.print("The result is ");
out.print(num);
out.println(" .");
}
}
And it works when I import this way:
当我以这种方式导入时它会起作用:
import static java.lang.System.out;
import static java.lang.System.*
But doesn't work when I try do this:
但是当我尝试这样做时不起作用:
import java.lang.System.out;
import java.lang.System.*
What's the meaning of the static
keyword in this particular case?
static
在这种特殊情况下,关键字的含义是什么?
And why import java.lang.*;
doesn't import the whole package with System
class in it?
为什么import java.lang.*;
不导入包含System
类的整个包?
回答by Oliver Charlesworth
A static
import allows you to write this:
一个static
进口允许你这样写:
out.print("The result is ");
rather than this:
而不是这个:
System.out.print("The result is ");
See e.g. http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html.
参见例如http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html。
回答by Jeff Olson
I often use static imports in my unit tests, like so:
我经常在我的单元测试中使用静态导入,如下所示:
import static org.junit.Assert.*;
This allows me to write this code:
这使我可以编写以下代码:
assertEquals(2, list.size());
Instead of this code:
而不是这个代码:
Assert.assertEquals(2, list.size());
回答by GingerHead
Static import is a feature introduced in the Java programming language that allows members (fields and methods) defined in a class as public static to be used in Java code without specifying the class in which the field is defined. This feature was introduced into the language in version 5.0.
静态导入是 Java 编程语言中引入的一项功能,它允许在类中定义为 public static 的成员(字段和方法)在 Java 代码中使用,而无需指定定义字段的类。这个特性是在 5.0 版本中引入的。
The feature provides a typesafe mechanism to include constants into code without having to reference the class that originally defined the field. It also helps to deprecate the practice of creating a constant interface: an interface that only defines constants then writing a class implementing that interface, which is considered an inappropriate use of interfaces.
该功能提供了一种类型安全机制,可以将常量包含在代码中,而无需引用最初定义该字段的类。它还有助于弃用创建常量接口的做法:一个只定义常量的接口,然后编写一个实现该接口的类,这被认为是对接口的不当使用。
When you import with static keyword it means you just inserted it somehow in your class and you can use it's methods the same way you're calling your own classes' methods.
当您使用 static 关键字导入时,这意味着您只是将它以某种方式插入到您的类中,并且您可以像调用您自己的类的方法一样使用它的方法。
For example:
例如:
import static java.lang.Math.*;
import static java.lang.System.out;
and :
和 :
out.println("I have a house with an area of " + (PI * pow(2.5,2)) + " sq. cm");
回答by Jet_C
Questions:
问题:
1)it works when I import this way:
1)当我以这种方式导入时它有效:
a. import static java.lang.System.out;
b. import static java.lang.System.*
But doesn't work when I try do this:
但是当我尝试这样做时不起作用:
c. import java.lang.System.out;
d. import java.lang.System.*;
2)What's the meaning of the static keyword in this particular case?
2)在这种特殊情况下 static 关键字的含义是什么?
3)And why import java.lang.*; doesn't import the whole package with System class in it?
3)以及为什么要导入 java.lang.*; 不导入带有 System 类的整个包?
--------------------------------------------------------------------------------
-------------------------------------------------- ------------------------------
Answers:
答案:
1) & 2)static imports e.g.(import static java.lang.System.out)are used to import methods or fields that have been declared as staticinside other classes in this particular case from the System class.
1) & 2)静态导入eg(import static java.lang.System.out)用于从 System 类导入在这种特殊情况下在其他类中声明为静态的方法或字段。
a. import static java.lang.System.out; //Works because "out" is a static field
b. import static java.lang.System.*; //Works because you are importing ALL static fields and methods inside the System class
c. import java.lang.System.out; //Does NOT work because you are trying to import a static field in a non-static way. (see a.)
d. import java.lang.System.*; //Actually Works because of the * wildcard which allows you to include all imports.
The main reason why you would want to import methods or fields in a static way is so you can omit the class specification for all calls to those methods or fields. So instead of writing:
您希望以静态方式导入方法或字段的主要原因是,您可以省略对这些方法或字段的所有调用的类规范。所以而不是写:
System.out.print("Hello");
System.out.print("World");
only write
只写
import static java.lang.System.* //or import static java.lang.System.out if you only plan on using the 'out' field.
out.print("Hello");
out.print("World");
3)importing java.lang.* is redundant! Java automatically and implicitly imports this package for you! :)and yes it imports the System class with it, but don't get confused unless you imported it as a static import you will still need to write out the long way:
3)导入java.lang.*是多余的!Java 会自动隐式地为你导入这个包!:)是的,它使用它导入 System 类,但不要混淆,除非您将其作为静态导入导入,否则您仍然需要写出很长的路:
System.out.print("hi");