Language Features
Packages
Complete lesson for Packages using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Packages
Section: Language Features. package and import, java.lang, static imports, access across packages, and classpath layout.
What is a package?
- A package is a namespace for types. The first line of the file is
package com.learncodeguru.java;(noimportbefore it except comments). - The folder path must match the package:
com/learncodeguru/java/Hello.java. Compile and run from the *source root*. - Convention: reverse-DNS, all lowercase, no underscores if you can avoid them.
package demo.util;
public class Greetings {
public static String hi(String name) {
return "Hello, " + name;
}
public static void main(String[] args) {
System.out.println(hi("Java"));
}
} import rules
import java.util.ArrayList;imports one type.import java.util.*;imports all types in that package, not subpackages.java.langis imported automatically. Subpackages likejava.lang.reflectare not.- Two classes with the same simple name (
java.util.Dateandjava.sql.Date) — import one and fully qualify the other.
Static import and access
import static java.lang.Math.PI;lets you writePIinstead ofMath.PI. Overuse hurts readability.- Package-private (no modifier) types and members are visible only inside the same package — a core encapsulation tool.
| Modifier | Same class | Same package | Subclass (other pkg) | World |
|---|---|---|---|---|
private | Yes | No | No | No |
| (package) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
Advanced — Packages
Modules, unnamed package, and classpath
- The default (unnamed) package cannot be imported. Do not leave real code there.
javac -d out src/demo/util/Greetings.javawrites class files underout/demo/util/. Runjava -cp out demo.util.Greetings.- Java 9+ modules (
module-info.java) add stronger encapsulation (exports). Interviews still start with packages + access modifiers. - Wildcard imports do not hurt runtime; they only affect compile-time name lookup.
Tricky Interview Points
Q1) What does a package do?
Answer It namespaces types, maps to a directory, and defines package-private visibility.
Q2) Can two public classes in different packages share a name?
Answer Yes. Fully qualify or import one. The JVM identity is package + class name.
Q3) Does import java.util.* import java.util.concurrent?
Answer No. Star imports only that package, not subpackages.
Q4) What is a static import?
Answer import static copies static members into simple-name scope, e.g. import static java.lang.Math.sqrt;
Q5) Where must the package statement appear?
Answer First in the file (after comments / Javadoc). Only one package declaration is allowed.
Q6) What is the default access modifier?
Answer Package-private: visible to all types in the same package, hidden from other packages.
Simple Programs
Same-file demo of package naming (default package fallback)
import static java.lang.Math.max;
public class PackageDemo {
public static void main(String[] args) {
System.out.println(max(3, 8));
java.util.List<String> names = java.util.List.of("a", "b");
System.out.println(names);
}
} Problem Solving Tricks
- Always declare a package in project code; default package is for scratch files only.
- IDE “run” hides classpath mistakes — practice
javac -dandjava -cponce. - Prefer single-type imports in tutorials so readers see where each class lives.
protectedis for subclasses *and* the same package — people forget the package part.
Practice Programs List
- Create
pkg.alpha.Aandpkg.beta.Bwhere B calls a public method of A. - Try to import a default-package class from a packaged class and read the error.
- Use a static import for
Math.powand print 2^10. - Make a package-private class and attempt to use it from another package.
- Compile with
-d outand run with-cp outfrom the project root.