2.3 Packages, Import Declarations & java.lang
Key Takeaways
Package hierarchies do not create shared namespaces—com.shop and com.shop.sales are completely independent packages, so importing com.shop.* does not import types in com.shop.sales
Code in a named package cannot import or refer directly to an unnamed-package class by its type name.
An on-demand import from a package makes its accessible top-level types available by simple name. It does not search subpackages and performs no run-time loading itself.
An explicit single-type import beats a wildcard import on name collisions, whereas two explicit single-type imports for the same simple name fail to compile even if the type is never referenced
Every Java source file implicitly receives import java.lang.*, giving direct access to String, System, Object, Math, and wrapper classes, but not subpackages like java.lang.reflect
Packages, Import Declarations & java.lang
Packages organize Java types into logical namespaces and prevent naming collisions across libraries. On the 1Z0-808 exam, package and import questions routinely test subtle compiler rules around wildcards, subpackages, import static, and ambiguous class names.
Package Hierarchy & the Unnamed Package
A package statement places every type in that .java file into the named namespace (commonly using reverse-domain convention, such as com.acme.inventory). Two package rules trip up many candidates:
- No nested namespace inheritance:
com.acmeandcom.acme.inventoryare unrelated packages to the compiler. Types incom.acme.inventorycannot see package-private members incom.acme, andimport com.acme.*;does not import types fromcom.acme.inventory. - The unnamed (default) package restriction: If a
.javafile omits apackagedeclaration, its classes reside in the default (unnamed) package. While a class in the default package can import classes from named packages, a class in a named package cannot import an unnamed-package type or refer to it by name. Because there is no package name to write in animportstatement, an unnamed-package type lacks the canonical name required for an import.
Single-Type, Wildcard, and static Imports
Import declarations tell javac how to resolve simple (unqualified) type or static member names. Imports participate in compile-time name resolution. Changing how you import a type has no inherent run-time cost. Verify that the import still resolves to the intended declaration.
| Import Statement | What It Imports | Legal? | Key Exam Rule |
|---|---|---|---|
import java.util.List; | Single type List | Yes | Overrides wildcard imports with same simple name |
import java.util.*; | Accessible top-level types directly in java.util | Yes | Does not import java.util.concurrent or java.util.regex |
import java.*; | Types directly in java (none in standard API) | Yes (compiles) | Does not import java.lang or java.util |
import static java.lang.Math.PI; | Static field PI from Math | Yes | Keyword order is import static, never static import |
import static java.lang.Math.*; | Accessible static members of Math, including fields, methods, and member types where present | Yes | Does not import non-static methods or Math itself |
import static java.lang.*; | Invalid—java.lang is a package | No | import static requires a type (Class.member or Class.*) |
Watch closely for static import versus import static and for accidentally putting import static on a package:
package metrics;
import static java.lang.Math.PI; // valid: static field
import static java.lang.Math.sqrt; // valid: static method (no parentheses!)
// static import java.lang.Math.*; // Does NOT compile: wrong keyword order
// import static java.util.*; // Does NOT compile: java.util is a package
public class Circle {
public static double radiusFromArea(double area) {
return sqrt(area / PI); // calls Math.sqrt and Math.PI directly
}
}
Notice two details in Circle: you never include parentheses () when importing a static method (sqrt, not sqrt()), and importing java.lang.Math.* statically does not import the class name Math itself (though Math happens to be available anyway via the implicit java.lang.* import).
Resolving Naming Collisions
Both java.util and java.sql contain a public class named Date. How the compiler reacts depends entirely on how you import them:
// Two wildcard imports. With no more specific declaration, Date is ambiguous.
import java.util.*;
import java.sql.*;
public class CalendarFix {
// Date d; // Does NOT compile: reference to Date is ambiguous
java.util.Date utilDate = new java.util.Date(); // FQN resolves ambiguity
java.sql.Date sqlDate = new java.sql.Date(0L);
}
Review the precedence rules side by side:
Combination in Same .java File | Does the File Compile? | Simple Name Date Resolves To |
|---|---|---|
Same-package Date + import java.util.*; | Yes | Same-package Date |
import java.sql.Date; + import java.util.*; | Yes | java.sql.Date (explicit beats wildcard) |
import java.util.Date; + import java.sql.Date; | No | Conflicting single-type imports are a compile-time error even if Date is unused |
import java.util.*; + import java.sql.*; (no unqualified Date) | Yes | Must use java.util.Date or java.sql.Date |
import java.util.*; + import java.sql.*; (uses Date unqualified) | No | Fails at usage line (reference to Date is ambiguous) |
When you need both java.util.Date and java.sql.Date in the same class, the clean solution is to import one explicitly (so you can refer to it as Date) and write the fully qualified name (FQN) for the other, or omit both imports and use FQNs for both.
Static Import Collisions
A similar ambiguity rule applies to import static: if you import MAX_VALUE statically from two different classes (import static pkg.Alpha.*; and import static pkg.Beta.*;), compilation succeeds until you reference MAX_VALUE unqualified, at which point javac rejects the reference as ambiguous. Two single-static imports of distinct fields can also coexist until an unqualified reference is ambiguous. For example, if both imports name a field MAX, use Alpha.MAX or Beta.MAX to select one.
The Implicit java.lang.* Import
Every Java source file automatically behaves as if import java.lang.*; were written at the top of the file. That is why you can use String, Object, System, Math, Integer, Double, Throwable, and RuntimeException without writing any import statement.
Tip
Explicitly writing import java.lang.*; or import java.lang.System; is redundant, not an error—it compiles cleanly. However, java.lang.* does not import subpackages such as java.lang.reflect.Method or java.lang.annotation.Retention; those still require explicit or wildcard imports.
Putting all of these rules together, remember that types in the same package as the current file are also implicitly visible without an import. Only types in other packages (aside from java.lang) require an import or an FQN.
Given the following import statements at the top of Report.java, what happens when you attempt to compile?
package reporting;
import java.util.*;
import java.sql.*;
import java.sql.Date;
public class Report {
Date created = new Date(0L);
}
Compilation fails because Date exists in both java.util and java.sql
Compilation fails because import java.sql.Date; is redundant with import java.sql.*;
It compiles cleanly, and created has type java.sql.Date
It compiles cleanly, and created has type java.util.Date
Which import statement fails to compile?
import java.lang.String;
import static java.lang.*;
import static java.lang.System.out;
import java.*;
A class Helper is declared in a file with no package statement. Another class Service is declared with package com.example;. How can Service use Helper?
It cannot refer to Helper directly by its type name from the named package
By writing import Helper; at the top of Service.java
By writing import default.Helper; at the top of Service.java
No import is needed because the unnamed package is implicitly visible everywhere
Sections you finish are checked off in the contents.