可能重复:如何访问默认包中的java-classes?
是可以在Java中导入默认包中的类吗?如果是这样,语法是什么?例如,如果您有
Is it possible to import a class in Java which is in the default package? If so, what is the syntax? For example, if you have
package foo.bar; public class SomeClass { // ...在一个文件中,你可以写
in one file, you can write
package baz.fonz; import foo.bar.SomeClass; public class AnotherClass { SomeClass sc = new SomeClass(); // ...在另一个文件中。但是,如果SomeClass.java不包含包声明呢?你如何在 AnotherClass 中引用 SomeClass ?
in another file. But what if SomeClass.java does not contain a package declaration? How would you refer to SomeClass in AnotherClass?
推荐答案您无法从默认包中导入类。除了非常小的示例程序之外,您应该避免使用默认包。
You can't import classes from the default package. You should avoid using the default package except for very small example programs.
来自 Java语言规范:
它是从未命名包导入类型时编译时间错误。
It is a compile time error to import a type from the unnamed package.
更多推荐
在Java中使用默认包导入类的语法是什么?
发布评论