Java package: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Tags: Mobile edit Mobile web edit
 
(93 intermediate revisions by 74 users not shown)
Line 1:
{{Short description|Package of Java software}}
A '''Java package''' organizes [[Java (programming language)|Java]] [[class (computer science)|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.
Classes in the same package can access each other's package-private and protected members.
Java packages can be stored in compressed files called [[JAR file]]s, allowing classes to be downloaded faster as groups rather than individually.
 
In general, a package can contain the following kinds of [[Datatypes|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.
 
==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.
 
<sourcesyntaxhighlight lang="java">
package java.awt.event;
</syntaxhighlight>
</source>
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
<sourcesyntaxhighlight lang="java">
import java.awt.event.*;
</syntaxhighlight>
</source>
imports all classes from the <code>java.awt.event</code> package, while the next declaration
<sourcesyntaxhighlight lang="java">
import java.awt.event.ActionEvent;
</syntaxhighlight>
</source>
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:
<sourcesyntaxhighlight lang="java">
ActionEvent myEvent = new ActionEvent();
</syntaxhighlight>
</source>
Classes can also be used directly without an import declaration by using the fully qualified name of the class. For example,
<sourcesyntaxhighlight lang="java">
java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();
</syntaxhighlight>
</source>
does not require a preceding import declaration.
 
===Package-wide Javadoc & annotations===
===The unnamed package===
 
NoteDocumentation thatexplaining ifthe youpackage do not useas a package declaration,whole youris classwritten endsas up[[Javadoc]] in ana unnamedfile named exactly `package-info.java`. ClassesThat infile anis unnamedalso packagethe cannotplace for annotations to be importedused byacross classesall inclasses anyof otherthe package.<ref>{{citeCite web|title=Chapter 7. Packages and Modules|url=httphttps://docs.oracle.com/javase/specs/jls/se7se11/html/jls-7.html#jls-7.5 4.1|titleaccess-date=Chapter 7. Packages 2021-12-10|publisherwebsite=Docsdocs.oracle.com |date= |accessdate=2013-09-15}}</ref>
 
===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.<ref>{{cite web|url=http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5 |title=Chapter 7. Packages |publisher=Docs.oracle.com |access-date=2013-09-15}}</ref> The official Java Tutorial advises against this:
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.<ref>{{Cite [web|url=https://docs.oracle.com/javase/tutorial/java/package/packages.html]|title=Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)|website=docs.oracle.com}}</ref>
 
==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 ''<code>protected</code>'' access modifier. Default (package-private) access is enforced when a class or member has not been declared as <code>public</code>, <code>protected</code> or <code>private</code>. By contrast, classes in other packages cannot access classes and members declared with default access. However, class members declared as <code>protected</code> can be accessed from the classes in the same package as well as classes in other packages that are subclasses of the declaring class.<ref>http{{Cite web|url=https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html|title=Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)|website=docs.oracle.com}}</ref>
 
==Creation of JAR files==
Line 54 ⟶ 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>
 
For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package <ttcode>ca.mysoft.fractions</ttcode> 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 <ttcode>de.mysoft.fractions</ttcode>, 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> or <code>javax</code> namespace, though some classes for XML parsing reside in <code>org.w3c</code>. Some projects, such as Project Lombok, do not even use TLDs at all (all symbols are located in namespace <code>lombok</code>).
 
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>http{{Cite web|url=https://docs.oracle.com/javase/specs/jls/se6/html/packages.html#7|title=Packages|website=docs.7oracle.com}}</ref>
 
==Core packages in Java SE 8==
{{main|Java Platform, Standard Edition}}
 
{| class="wikitable plainrowheaders"
{|
! scope="row" | {{code|java.lang}}
| java.lang || — basicBasic language functionality and fundamental types. Implicitly imported by every program.
|-
! scope="row" | {{code|java.util}}
| java.util || — collectionCollection [[data structure]] classes
|-
! scope="row" | {{code|java.io}}
| java.io || — fileFile operations
|-
! scope="row" | {{code|java.math}}
| java.math || — multiprecision arithmetics
| Multiprecision arithmetics
|-
! scope="row" | {{code|java.nio}}
| java.nio || — theThe [[Non-blocking I/O (Java)|Non-blocking I/O]] framework for Java
|-
! scope="row" | {{code|java.net}}
| java.net || — networkingNetworking operations, sockets, [[DNS lookup]]s, ...
|-
! scope="row" | {{code|java.security }}
|| — keyKey generation, encryption and decryption
|-
! scope="row" | {{code|java.sql }}
|| — [[Java Database Connectivity]] (JDBC) to access databases
|-
! scope="row" | {{code|java.awt }}
|| — basicBasic hierarchy of packages for native GUI components
|-
! scope="row" | {{code|java.text}}
| javax.swing || — hierarchy of packages for platform-independent rich [[GUI]] components
| java.text || — Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
|-
! scope="row" | {{code|java.rmi}}
| java.applet || — classes for creating an applet
| java.rmi || — Provides the RMI package.
|-
! scope="row" | {{code|java.time}}
| java.beans || — Contains classes related to developing beans -- components based on the JavaBean architecture.
| java.time || — The main API for dates, times, instants, and durations.
|-
! scope="row" | {{code|java.beans}}
| java.text || — Provides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
| The java.beans package contains classes and interfaces related to JavaBeans components.
|-
! scope="row" | {{code|java.applet}}
| java.rmi || — Provides the RMI package.
| This package provides classes and methods to create and communicate with the applets.
|-
| java.time || — The main API for dates, times, instants, and durations.
|}
 
The java.lang package is available without the use of an import statement.
 
==Modules==
{{details|Java Platform Module System}}
In [[Java 9]] (released on September 21, 2017) support for "[[modular programming|modules]]", a kind of collection of packages, arewas plannedimplemented as parta result of the development effort of Project Jigsaw;. theseThe "modules" were earlier called "superpackages" and originally planned for Java 7.
 
Modules will describe their dependencies in a module declaration which will be placed in a file named ''module-info.java'' at the root of the module’smodule's source-file hierarchy. TheSince JDK[[Java willversion history#Java SE 9|Java 9]], the JDK beis able to check themthe module dependencies both at compile- time and runtime. The JDK itself will beis 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
| title=JDK Module Summary
| publisher=[[Oracle Corporation]]
| date=2015-10-23
| accessdateaccess-date=2015-11-29}}</ref>
| archive-date=2015-12-08
| archive-url=https://web.archive.org/web/20151208074800/http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html
| 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=[[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==
Line 113 ⟶ 144:
 
[[Category:Java (programming language)|Package]]
[[Category:Modularity]]