Java package: Difference between revisions

From HandWiki
imported>Pchauhan2001
correction
 
WikiEd2 (talk | contribs)
simplify
 
Line 1: Line 1:
{{Short description|Package of Java software}}
A '''Java package''' organizes [[Java (programming language)|Java]] classes into namespaces,<ref>James Gosling, Bill Joy, Guy Steele, Gilad Bracha, ''The Java Language Specification, Third Edition'', {{ISBN|0-321-24678-0}}, 2005. In the Introduction, it is stated "Chapter 7 describes the structure of a program, which is organized into packages similar to the modules of Modula."</ref>  
A '''Java package''' organizes [[Java (programming language)|Java]] classes into namespaces,<ref>James Gosling, Bill Joy, Guy Steele, Gilad Bracha, ''The Java Language Specification, Third Edition'', {{ISBN|0-321-24678-0}}, 2005. In the Introduction, it is stated "Chapter 7 describes the structure of a program, which is organized into packages similar to the modules of Modula."</ref>  
providing a unique namespace for each type it contains.  
providing a unique namespace for each type it contains.  
Classes in the same package can access each other's package-private and protected members.
Classes in the same package can access each other's package-private and protected members.


In general, a package can contain the  following kinds of types: classes, [[Interface (Java)|interfaces]], enumerations, and [[Java annotation|annotation]] types. A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all have to do with a specific application or perform a specific set of tasks.
In general, a package can contain the  following kinds of types: classes, [[Interface (Java)|interfaces]], enumerations, records and [[Java annotation|annotation]] types. A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all have to do with a specific application or perform a specific set of tasks.
Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.
Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.


==Using packages==
==Using packages==
In a Java source file, the package that this file's class or classes belong to is specified with the <code>package</code> [[Keyword (computer programming)|keyword]]. This keyword is usually the first keyword in the source file. At most one package declaration can appear in a source file.
In a Java source file, the package that this file's class or classes belong to is specified with the  
<code>package</code> [[Keyword (computer programming)|keyword]]. This keyword is usually the first keyword in the source file. At most one package declaration can appear in a source file.


<source lang="java">
<syntaxhighlight lang="java">
package java.awt.event;
package java.awt.event;
</source>
</syntaxhighlight>
To use a package's classes inside a Java source file, it is convenient to import the classes from the package with an <code>import</code> declaration. The following declaration
To use a package's classes inside a Java source file, it is convenient to import the classes from the package with an <code>import</code> declaration, which dequalifies the namespaces of the class into scope. The following declaration
<source lang="java">
<syntaxhighlight lang="java">
import java.awt.event.*;
import java.awt.event.*;
</source>
</syntaxhighlight>
imports all classes from the <code>java.awt.event</code> package, while the next declaration
imports all classes from the <code>java.awt.event</code> package, while the next declaration
<source lang="java">
<syntaxhighlight lang="java">
import java.awt.event.ActionEvent;
import java.awt.event.ActionEvent;
</source>
</syntaxhighlight>
imports only the <code>ActionEvent</code> class from the package. After either of these import declarations, the <code>ActionEvent</code> class can be referenced using its simple class name:
imports only the <code>ActionEvent</code> class from the package. After either of these import declarations, the <code>ActionEvent</code> class can be referenced using its simple class name:
<source lang="java">
<syntaxhighlight lang="java">
ActionEvent myEvent = new ActionEvent();
ActionEvent myEvent = new ActionEvent();
</source>
</syntaxhighlight>
Classes can also be used directly without an import declaration by using the fully qualified name of the class. For example,
Classes can also be used directly without an import declaration by using the [[Fully qualified name|fully qualified name]] of the class. For example,
<source lang="java">
<syntaxhighlight lang="java">
java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();
java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();
</source>
</syntaxhighlight>
does not require a preceding import declaration.
does not require a preceding import declaration.


===Package-wide Javadoc & annotations===
===Package-wide Javadoc & annotations===


Documentation explaining the package as a whole is written as [[Software:Javadoc|Javadoc]] in a file named exactly `package-info.java`. That file is also the place for annotations to be used across all classes of the package.<ref>{{Cite web|title=Chapter 7. Packages and Modules|url=https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1|access-date=2021-12-10|website=docs.oracle.com}}</ref>
Documentation explaining the package as a whole is written as [[Software:Javadoc|Javadoc]] in a file named exactly {{mono|package-info.java}}. That file is also the place for annotations to be used across all classes of the package.<ref>{{Cite web|title=Chapter 7. Packages and Modules|url=https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1|access-date=2021-12-10|website=docs.oracle.com}}</ref>


===The unnamed package===
===The unnamed package===
Line 55: Line 57:
In general, a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its package. Subsequent components of the package name vary according to an organization's own internal naming conventions.<ref>[http://www.oracle.com/technetwork/java/codeconventions-135099.html Code Conventions for the Java Programming Language: 9. Naming Conventions]</ref>
In general, a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its package. Subsequent components of the package name vary according to an organization's own internal naming conventions.<ref>[http://www.oracle.com/technetwork/java/codeconventions-135099.html Code Conventions for the Java Programming Language: 9. Naming Conventions]</ref>


For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package {{mono|ca.mysoft.fractions}} distinguishes the fractions package from another similar package created by another company. If a German company named MySoft also creates a fractions package, but names it {{mono|de.mysoft.fractions}}, then the classes in these two packages are defined in a unique and separate namespace.
For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package <code>ca.mysoft.fractions</code> distinguishes the fractions package from another similar package created by another company. If a German company named MySoft also creates a fractions package, but names it <code>de.mysoft.fractions</code>, then the classes in these two packages are defined in a unique and separate namespace. There is no requirement that the TLD must be the name of the country of the company. For instance, many packages also use other TLDs such as <code>org</code>, <code>com</code>, etc. The Java standard library puts all symbols in the <code>java.*</code>, <code>javax.*</code>, or <code>jdk.*</code> namespace, though some classes associated with other technologies may [[Java Class Library#Packages outside of the main namespace|reside in other namespaces]]. Some projects, such as Project Lombok, may not use TLDs at all (all symbols are located in namespace <code>lombok</code>).
 
Using TLDs in package names is a convention primarily associated with Java, but seldom used in other languages. In other languages such as [[C++]] and [[C Sharp (programming language)|C#]] it is sufficient to just prefix package/namespace names with the company name.


Complete conventions for disambiguating package names and rules for naming packages when the Internet domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification.<ref>{{Cite web|url=https://docs.oracle.com/javase/specs/jls/se6/html/packages.html|title=Packages|website=docs.oracle.com}}</ref>
Complete conventions for disambiguating package names and rules for naming packages when the Internet domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification.<ref>{{Cite web|url=https://docs.oracle.com/javase/specs/jls/se6/html/packages.html|title=Packages|website=docs.oracle.com}}</ref>
Line 63: Line 67:


{| class="wikitable plainrowheaders"
{| class="wikitable plainrowheaders"
! scope="row" | java.lang
! scope="row" | {{code|java.lang}}
| basic language functionality and fundamental types that is available without the use of an import statement.
| Basic language functionality and fundamental types. Implicitly imported by every program.
|-
|-
! scope="row" | java.util
! scope="row" | {{code|java.util}}
| collection [[Data structure|data structure]] classes
| Collection [[Data structure|data structure]] classes
|-
|-
! scope="row" | java.io
! scope="row" | {{code|java.io}}
| file operations
| File operations
|-
|-
! scope="row" | java.math
! scope="row" | {{code|java.math}}
| multiprecision arithmetics
| Multiprecision arithmetics
|-
|-
! scope="row" | java.nio
! scope="row" | {{code|java.nio}}
| the Non-blocking I/O framework for Java
| The Non-blocking I/O framework for Java
|-
|-
! scope="row" | java.net
! scope="row" | {{code|java.net}}
| networking operations, sockets, DNS lookups, ...
| Networking operations, sockets, DNS lookups, ...
|-
|-
! scope="row" | java.security
! scope="row" | {{code|java.security}}
| key generation, encryption and decryption
| Key generation, encryption and decryption
|-
|-
! scope="row" | java.sql
! scope="row" | {{code|java.sql}}
| [[Software:Java Database Connectivity|Java Database Connectivity]] (JDBC) to access databases
| [[Software:Java Database Connectivity|Java Database Connectivity]] (JDBC) to access databases
|-
|-
! scope="row" | java.awt
! scope="row" | {{code|java.awt}}
| basic hierarchy of packages for native GUI components
| Basic hierarchy of packages for native GUI components
|-
|-
! scope="row" | java.text
! scope="row" | {{code|java.text}}
| Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
| Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
|-
|-
! scope="row" | java.rmi
! scope="row" | {{code|java.rmi}}
| Provides the RMI package.
| Provides the RMI package.
|-
|-
! scope="row" | java.time
! scope="row" | {{code|java.time}}
| The main API for dates, times, instants, and durations.
| The main API for dates, times, instants, and durations.
|-
|-
! scope="row" | java.beans
! scope="row" | {{code|java.beans}}
| The java.beans package contains classes and interfaces related to JavaBeans components.
| The java.beans package contains classes and interfaces related to JavaBeans components.
|-
|-
! scope="row" | java.applet
! scope="row" | {{code|java.applet}}
| This package provides classes and methods to create and communicate with the applets.
| This package provides classes and methods to create and communicate with the applets.
|}
|}
Line 108: Line 112:
==Modules==
==Modules==
{{details|Java Platform Module System}}
{{details|Java Platform Module System}}
In Java 9 (released on September 21, 2017) support for "modules", a kind of collection of packages, was implemented as a result of the development effort of Project Jigsaw. The "modules" were earlier called "superpackages" and originally planned for Java 7.
In Java 9 (released on September 21, 2017) support for "[[Modular programming|modules]]", a kind of collection of packages, was implemented as a result of the development effort of Project Jigsaw. The "modules" were earlier called "superpackages" and originally planned for Java 7.


Modules describe their dependencies in a declaration placed in a file named ''module-info.java'' at the root of the module's source-file hierarchy. Since [[Java version history#Java SE 9|Java 9]], the JDK is able to check the module dependencies both at compile time and runtime. The JDK itself is modularized for [[Java version history#Java SE 9|Java 9]].<ref>{{cite web
Modules describe their dependencies in a declaration placed in a file named ''module-info.java'' at the root of the module's source-file hierarchy. Since [[Java version history#Java SE 9|Java 9]], the JDK is able to check the module dependencies both at [[Compile time|compile time]] and runtime. The JDK itself is modularized for [[Java version history#Java SE 9|Java 9]].<ref>{{cite web
| url=http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html
| url=http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html
| title=JDK Module Summary
| title=JDK Module Summary
Line 119: Line 123:
| archive-url=https://web.archive.org/web/20151208074800/http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html
| archive-url=https://web.archive.org/web/20151208074800/http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html
| url-status=dead
| url-status=dead
}}</ref><ref>{{cite web |url=https://www.oracle.com/corporate/features/understanding-java-9-modules.html |title=Understanding Java 9 Modules | publisher=[[Company:Oracle Corporation|Oracle Corporation]]| date=October 1, 2017| access-date=2022-10-04}}</ref>
}}</ref><ref>{{cite web |url=https://www.oracle.com/corporate/features/understanding-java-9-modules.html |title=Understanding Java 9 Modules | publisher=[[Company:Oracle Corporation|Oracle Corporation]]| date=October 1, 2017| access-date=2022-10-04}}</ref> For example, the majority of the Java standard library is exported by the module <code>java.base</code>.
 
As an example, the following module declaration declares that the module ''com.foo.bar'' depends on another ''com.foo.baz'' module, and exports the following 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==
* [[Translation unit (programming)]]
* Modules (C++)


==References==
==References==
Line 128: Line 146:


[[Category:Java (programming language)|Package]]
[[Category:Java (programming language)|Package]]
[[Category:Modularity]]


{{Sourceattribution|Java package}}
{{Sourceattribution|Java package}}

Latest revision as of 07:27, 17 May 2026

Short description: Package of Java software

A Java package organizes Java classes into namespaces,[1] providing a unique namespace for each type it contains. Classes in the same package can access each other's package-private and protected members.

In general, a package can contain the following kinds of types: classes, interfaces, enumerations, records and annotation types. A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all have to do with a specific application or perform a specific set of tasks. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.

Using packages

In a Java source file, the package that this file's class or classes belong to is specified with the package keyword. This keyword is usually the first keyword in the source file. At most one package declaration can appear in a source file.

package java.awt.event;

To use a package's classes inside a Java source file, it is convenient to import the classes from the package with an import declaration, which dequalifies the namespaces of the class into scope. The following declaration

import java.awt.event.*;

imports all classes from the java.awt.event package, while the next declaration

import java.awt.event.ActionEvent;

imports only the ActionEvent class from the package. After either of these import declarations, the ActionEvent class can be referenced using its simple class name:

ActionEvent myEvent = new ActionEvent();

Classes can also be used directly without an import declaration by using the fully qualified name of the class. For example,

java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();

does not require a preceding import declaration.

Package-wide Javadoc & annotations

Documentation explaining the package as a whole is written as Javadoc in a file named exactly package-info.java. That file is also the place for annotations to be used across all classes of the package.[2]

The unnamed package

If a package declaration is not used, classes are placed in an unnamed package. Classes in an unnamed package cannot be imported by classes in any other package.[3] The official Java Tutorial advises against this:

Generally speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. Otherwise, classes and interfaces belong in named packages.[4]

Package access protection

Public members and classes are visible everywhere and private members are visible only in the same class. Classes within a package can access classes and members declared with default (package-private) access as well as class members declared with the protected access modifier. Default (package-private) access is enforced when a class or member has not been declared as public, protected or private. By contrast, classes in other packages cannot access classes and members declared with default access. However, class members declared as protected can be accessed from the classes in the same package as well as classes in other packages that are subclasses of the declaring class.[5]

Creation of JAR files

JAR files are created with the jar command-line utility. The command

jar cf myPackage.jar *.class

compresses all .class files into the JAR file myPackage.jar. The 'c' option on the command line tells the jar command to "create new archive." The ' f ' option tells it to create a file. The file's name comes next before the contents of the JAR file.

Package naming conventions

Packages are usually defined using a hierarchical naming pattern, with some levels in the hierarchy separated by periods (., pronounced "dot"). Although packages lower in the naming hierarchy are often referred to as "subpackages" of the corresponding packages higher in the hierarchy, there is almost no semantic relationship between packages. The Java Language Specification establishes package naming conventions to avoid the possibility of two published packages having the same name. The naming conventions describe how to create unique package names, so that packages that are widely distributed will have unique namespaces. This allows packages to be separately, easily and automatically installed and catalogued.

In general, a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its package. Subsequent components of the package name vary according to an organization's own internal naming conventions.[6]

For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package ca.mysoft.fractions distinguishes the fractions package from another similar package created by another company. If a German company named MySoft also creates a fractions package, but names it de.mysoft.fractions, then the classes in these two packages are defined in a unique and separate namespace. There is no requirement that the TLD must be the name of the country of the company. For instance, many packages also use other TLDs such as org, com, etc. The Java standard library puts all symbols in the java.*, javax.*, or jdk.* namespace, though some classes associated with other technologies may reside in other namespaces. Some projects, such as Project Lombok, may not use TLDs at all (all symbols are located in namespace lombok).

Using TLDs in package names is a convention primarily associated with Java, but seldom used in other languages. In other languages such as C++ and C# it is sufficient to just prefix package/namespace names with the company name.

Complete conventions for disambiguating package names and rules for naming packages when the Internet domain name cannot be directly used as a package name are described in section 7.7 of the Java Language Specification.[7]

Core packages in Java SE 8

java.lang Basic language functionality and fundamental types. Implicitly imported by every program.
java.util Collection data structure classes
java.io File operations
java.math Multiprecision arithmetics
java.nio The Non-blocking I/O framework for Java
java.net Networking operations, sockets, DNS lookups, ...
java.security Key generation, encryption and decryption
java.sql Java Database Connectivity (JDBC) to access databases
java.awt Basic hierarchy of packages for native GUI components
java.text Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
java.rmi Provides the RMI package.
java.time The main API for dates, times, instants, and durations.
java.beans The java.beans package contains classes and interfaces related to JavaBeans components.
java.applet This package provides classes and methods to create and communicate with the applets.

Modules

In Java 9 (released on September 21, 2017) support for "modules", a kind of collection of packages, was implemented as a result of the development effort of Project Jigsaw. The "modules" were earlier called "superpackages" and originally planned for Java 7.

Modules describe their dependencies in a declaration placed in a file named module-info.java at the root of the module's source-file hierarchy. Since Java 9, the JDK is able to check the module dependencies both at compile time and runtime. The JDK itself is modularized for Java 9.[8][9] For example, the majority of the Java standard library is exported by the module java.base.

As an example, the following module declaration declares that the module com.foo.bar depends on another com.foo.baz module, and exports the following packages: com.foo.bar.alpha and com.foo.bar.beta:

module com.foo.bar {
    requires com.foo.baz;

    exports com.foo.bar.alpha;
    exports com.foo.bar.beta;
}

See also

References

  1. James Gosling, Bill Joy, Guy Steele, Gilad Bracha, The Java Language Specification, Third Edition, ISBN 0-321-24678-0, 2005. In the Introduction, it is stated "Chapter 7 describes the structure of a program, which is organized into packages similar to the modules of Modula."
  2. "Chapter 7. Packages and Modules". https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.4.1. 
  3. "Chapter 7. Packages". Docs.oracle.com. http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5. 
  4. "Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)". https://docs.oracle.com/javase/tutorial/java/package/packages.html. 
  5. "Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)". https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html. 
  6. Code Conventions for the Java Programming Language: 9. Naming Conventions
  7. "Packages". https://docs.oracle.com/javase/specs/jls/se6/html/packages.html. 
  8. "JDK Module Summary". Oracle Corporation. 2015-10-23. http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html. 
  9. "Understanding Java 9 Modules". Oracle Corporation. October 1, 2017. https://www.oracle.com/corporate/features/understanding-java-9-modules.html.