What's the syntax to import a class in a default package in Java? [duplicate]
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();
// ...
in another file. But what if SomeClass.java does not contain a package declaration? How would you refer to SomeClass
in AnotherClass
?
Solution 1:
You can't import classes from the default package. You should avoid using the default package except for very small example programs.
From the Java language specification:
It is a compile time error to import a type from the unnamed package.
Solution 2:
The only way to access classes in the default package is from another class in the default package. In that case, don't bother to import
it, just refer to it directly.