Java packageA 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 packagesIn a Java source file, the package that this file's class or classes belong to is specified with the
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 java.awt.event.*;
imports all classes from the import java.awt.event.ActionEvent;
imports only the 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 & annotationsDocumentation 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 packageIf 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:
Package access protectionPublic 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 Creation of JAR filesJAR 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 conventionsPackages are usually defined using a hierarchical naming pattern, with some levels in the hierarchy separated by periods ( 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. 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
ModulesIn 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] References
External links |
Portal di Ensiklopedia Dunia