Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

The keyword package

The keyword package: A package declaration assigns at most one package name to an ordinary compilation unit and namespaces its top-level types.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

package

A package declaration assigns at most one package name to an ordinary compilation unit and namespaces its top-level types.

Organize stable APIs under reverse-domain names and align source layout and module exports with package boundaries.

No declaration means the unnamed package, which production code should avoid; a package name provides neither security nor ownership.

Package-private access requires the same run-time package, which includes both the package name and the defining class loader.

Main is in the unnamed package as main-package=, while String and String[] both report java.lang.

Examples

public class Main { public static void main(String[] a){System.out.println("main-package="+Main.class.getPackageName());System.out.println("string-package="+String.class.getPackageName());System.out.println("array-package="+String[].class.getPackageName());} }

Result

main-package=
string-package=java.lang
array-package=java.lang

Main is in the unnamed package as main-package=, while String and String[] both report java.lang.

Sources