Java“静态导入”与“导入静态”相对在Java 8中 [英] Java "static import" vs. "import static" in Java 8
问题描述
我试图在Java上使用静态导入,但我写错了
I was trying to use use static imports on Java, but I was writing it wrong
static import java.lang.System.out;
编译的代码(虽然找不到out符号),没有语法错误。
And the code compiled (although the "out" symbol couldn't be found), no syntax errors.
那么,静态导入实际上意味着什么?
So, what does the "static import" actually means?
推荐答案
这不应该编译。
static import java.lang.System.out;
根据JLS ,单个静态导入应如下所示:
According to the JLS, a single static import should look like this:
import static java.lang.System.out;
所有形式的Java导入语句都以 <$ c $开头c> import 关键字,我不认为 import
关键字可以有任何其他上下文(即除了import语句)使用。
All forms of the Java import statement start with the import
keyword, and I don't think there is any other context (i.e. apart from an import statement) in which the import
keyword can be used.
注意: import
和 static
关键字在此上下文中不是修饰符,因此修饰符可以按任何顺序提供元规则不适用于此。
Note: the import
and static
keywords are not modifiers in this context, so the "modifiers can be supplied in any order" meta-rule does not apply here.
简而言之,您的编译器/ IDE被破坏或混淆......或者您所看到的不是真正的Java源代码。
In short, either your compiler / IDE is broken or confused ... or what you are looking at is not real Java source code.
这篇关于Java“静态导入”与“导入静态”相对在Java 8中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!