Week 56 — What is the unnamed package and what is special about it?
Question of the Week #56
What is the unnamed package and what is special about it?
3 Replies
Normally, Java classes are located in packages. This can be done by creating subdirectories in the source folder and creating the classes in these subdirectories.
Classes within packages need to start with
package
followed by the name of the package which is a list of nested subdirectories separated by .
ending with a ;
.
For example, a class Hello
in a directory com/mypackage/hello
could look like:
If a class is located directly in the source folder, it is in the unnamed package. Classes in the unnamed package must not have a
package
declaration.
Classes within the unnamed package cannot be imported. However, it is possible that a class in the unnamed package imports a class from a (named) package.
Aside from that, these classes cannot be part of any module except the unnamed module (it is not possible to have a module-info.java
and classes in the same directory).📖 Sample answer from dan1st
It's a special package that doesn't have a name. It is the package to which all classes belong that are not explicitly placed in a named package using the statement.
Submission from thehsb