Interview Q&A · Java · 6 questions in this topic
Packages: Rules and Structure — Java Interview Q&A
How packages organize types, prevent name clashes, and control what source files may declare. Part of OOP Mechanics · 294 questions in the Java bank.
What are packages in Java?
Answer
A package groups related classes, interfaces, enums, and records into one named module and defines a namespace for them. That grouping resolves naming conflicts, because types such as com.shop.Order and com.blog.Order can coexist with full names. It also supports visibility control through package-private access, and by convention package names are lowercase reverse-domain names.
Key points
- 1Groups related types into one module for organization.
- 2Prevents name clashes through qualified namespaces.
- 3Enables package-private visibility for controlled sharing.
Can a source file have more than one package statement?
Answer
No, a compilation unit can have at most one package statement. Declaring two package statements in the same source file is a compile-time error because every top-level type in the file must belong to exactly one package.
Can the package statement come after import?
Answer
No, the package declaration must be the first non-comment statement in the file. The required order is an optional package statement, then import statements, then type declarations, so placing an import before the package causes a compile-time error.
Example
package com.example.shop; import java.util.List; public class Order {}
Must a package name match the source directory structure?
Answer
Not for compilation: javac reads the package declaration from the file, so a source file can sit anywhere and still compile into that package. The class files, however, must end up in directories that mirror the package, because the JVM and the class loader locate com.example.Order only at com/example/Order.class. Use javac -d out to have the compiler create that layout automatically rather than moving files by hand.
Key points
- 1The package statement, not the folder, decides the type’s package.
- 2The JVM requires .class files in a directory tree matching the package.
- 3javac -d builds that tree for you.
- 4IDEs keep sources and output trees aligned so this rarely surfaces.
Example
javac -d out src/com/example/Order.java # produces out/com/example/Order.class
What is the unnamed (default) package and why avoid it?
Answer
A type declared without a package statement lives in the unnamed package. Types there cannot be imported by named packages, so code in a real package cannot refer to them except through reflection, and they cannot be modularised. That makes the unnamed package fine for a single throwaway file and unsuitable for anything shared, which is why libraries and applications always declare a package.
Key points
- 1No package statement means the unnamed package.
- 2Named packages cannot import from it.
- 3Java modules cannot contain it.
- 4It is acceptable only for scratch programs.
What is a split package, and why does it matter?
Answer
A split package is one package name spread across several JARs or modules. On the classpath it works but is fragile: which copy wins depends on classpath order, and package-private access suddenly spans unrelated code. In the module system it is a hard error — two modules may not export the same package — so the fix is to move the classes into distinct packages or merge them into one module.
Key points
- 1Split packages spread one package name across artefacts.
- 2Classpath order decides which version is loaded.
- 3JPMS rejects split packages between modules.
- 4Renaming or merging the package restores a clean boundary.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission