Jump to content

Java package

From Alfawiki
Java package
Company typeProgramming language feature
IndustrySoftware development
ProductsJava classes, interfaces, enumerations, records, annotation types

A Java package serves to categorize Java classes within distinct namespaces, ensuring each contained type has a unique namespace. Classes grouped in the same package have the ability to access one another’s members with package-private and protected visibility.

Packages may include classes, interfaces, enumerations, records, and annotation types. They function to cluster related classes, typically associated with particular functionalities or applications, thereby facilitating effective code organization for developers.

Using packages

In a Java source file, the package declaration is made using the package keyword, usually positioned as the initial statement. Each source file can declare only one package.

<syntaxhighlight lang="java"> package java.awt.event; </syntaxhighlight> To utilize classes from a specific package, programmers often employ an import statement:

<syntaxhighlight lang="java"> import java.awt.event.*; </syntaxhighlight> brings in all classes from java.awt.event, whereas

<syntaxhighlight lang="java"> import java.awt.event.ActionEvent; </syntaxhighlight> imports solely the ActionEvent class. Once imported, these classes can be referenced by their simple names:

<syntaxhighlight lang="java"> ActionEvent myEvent = new ActionEvent(); </syntaxhighlight> Alternatively, without using import statements, fully qualified class names can be used directly:

<syntaxhighlight lang="java"> java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent(); </syntaxhighlight>

Package-wide Javadoc & annotations

Comprehensive documentation for a package is authored as Javadoc within a file named `package-info.java`, which also holds annotations that apply to all classes in the package.

The unnamed package

Classes that lack a package declaration belong to an unnamed package. Such packages cannot be imported by others and are generally discouraged except for small-scale projects, temporary code, or initial development phases. For better structure and scalability, named packages are recommended.

Package access protection

Members and classes declared as public are accessible universally, while private members remain confined to their own class. Within a package, classes can access members with default (package-private) visibility and those marked with the protected modifier. Default access is implied when no explicit public, protected, or private modifier is present. Members with default access are inaccessible outside their package; however, protected members can be accessed by subclasses even if they reside in different packages, as well as by other classes within the same package.

Creation of JAR files

JAR archives are generated using the jar command-line tool. For instance,

jar cf myPackage.jar *.class

bundles all .class files into myPackage.jar. The 'c' flag instructs creation of a new archive, while 'f' designates the filename, followed by files to include.

Package naming conventions

Packages utilize a hierarchical naming scheme separated by periods (.). Although lower-level packages are commonly referred to as "subpackages," they do not possess any inherent semantic connection to their parent packages. To prevent naming collisions, the Java Language Specification establishes guidelines that guarantee unique package namespaces for distribution and organization.

Package names generally begin with the organization's top-level domain, followed by its domain and any subdomains in reverse sequence, then conclude with a specific package identifier based on internal naming rules. For instance, a Canadian firm named MySoft might designate a fractions package as , differentiating it from a German company's package. This approach ensures namespace uniqueness across different organizations.

Additional instructions for resolving name conflicts and addressing situations where domain names cannot be directly applied are outlined in section 7.7 of the Java Language Specification.

Core packages in Java SE 8

<syntaxhighlight lang="text" class="" style="" inline="1">java.lang</syntaxhighlight> Provides essential language features and types; automatically imported into all Java programs.
<syntaxhighlight lang="text" class="" style="" inline="1">java.util</syntaxhighlight> Classes for collection data structures
<syntaxhighlight lang="text" class="" style="" inline="1">java.io</syntaxhighlight> Functionality related to file handling
<syntaxhighlight lang="text" class="" style="" inline="1">java.math</syntaxhighlight> Support for arbitrary-precision arithmetic operations
<syntaxhighlight lang="text" class="" style="" inline="1">java.nio</syntaxhighlight> Framework for non-blocking input/output operations
<syntaxhighlight lang="text" class="" style="" inline="1">java.net</syntaxhighlight> Networking functionalities including sockets and DNS resolution
<syntaxhighlight lang="text" class="" style="" inline="1">java.security</syntaxhighlight> Facilities for key generation, encryption, and decryption
<syntaxhighlight lang="text" class="" style="" inline="1">java.sql</syntaxhighlight> JDBC API enabling database interactions
<syntaxhighlight lang="text" class="" style="" inline="1">java.awt</syntaxhighlight> Hierarchy of native graphical user interface components
<syntaxhighlight lang="text" class="" style="" inline="1">java.text</syntaxhighlight> Handling of text, dates, numbers, and messages independent of locale
<syntaxhighlight lang="text" class="" style="" inline="1">java.rmi</syntaxhighlight> Support for Remote Method Invocation (RMI)
<syntaxhighlight lang="text" class="" style="" inline="1">java.time</syntaxhighlight> API for managing dates, times, instants, and durations
<syntaxhighlight lang="text" class="" style="" inline="1">java.beans</syntaxhighlight> Interfaces and classes related to JavaBeans components
<syntaxhighlight lang="text" class="" style="" inline="1">java.applet</syntaxhighlight> Tools for creating and interacting with applets

Modules

The Java Platform Module System was introduced in Java 9 (September 2017) to facilitate modular programming by organizing packages into modules—a concept developed as part of Project Jigsaw. Formerly called "superpackages," modules specify dependencies within a module-info.java file located at the root of the source directory. From Java 9 onward, the JDK enforces these dependencies both during compilation and runtime, with the JDK itself structured modularly.

For example, the fundamental Java standard library is primarily provided by the java.base module.

An example module declaration illustrates com.foo.bar requiring com.foo.baz while exporting the packages com.foo.bar.alpha and com.foo.bar.beta: <syntaxhighlight lang="cpp"> module com.foo.bar {

   requires com.foo.baz;
   exports com.foo.bar.alpha;
   exports com.foo.bar.beta;

} </syntaxhighlight>

See also

External links