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
?
Java compiler imports java. lang package internally by default.
Otherwise, it is very easy. In Eclipse or NetBeans just write the class you want to use and press on Ctrl + Space . The IDE will automatically import the class.
The default package is a collection of java classes whose source files do not contain and package declarations. These packages act as the default package for such classes. It provides the ease of creating small applications when the development of any project or application has just begun.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With