Java package
Encyclopedia
A Java package is a mechanism for organizing Java
classes
into namespaces
similar to the modules of Modula
. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.
A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all be to do with a specific application or perform a specific set of tasks. For example, the Java API is full of packages. One of them is the javax.xml package. It and its subpackages contain all the classes in the Java API to do with handling XML.
. This keyword is usually the first keyword in the source file.
To use a package's classes inside a Java source file, it is convenient to import the classes from the package with an
imports all classes from the
imports only the
Classes can also be used directly without an import declaration by using the fully qualified name of the class. For example,
does not require a preceding import declaration.
Note that if you do not use a package declaration, your class ends up in an unnamed package.
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.
, with 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. Package names should be all lowercase characters whenever possible.
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.
The package is available without the use of an import statement.
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
classes
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
into namespaces
Namespace (computer science)
A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols . An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces...
similar to the modules of Modula
Modula
The Modula programming language is a descendent of the Pascal programming language. It was developed in Switzerland in the late 1970s by Niklaus Wirth, the same person who designed Pascal...
. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.
- A package provides a unique namespace for the types it contains.
- Classes in the same package can access each other's package-access members.
Overview
In general, a package can contain the following kinds of types.A package allows a developer to group classes (and interfaces) together. These classes will all be related in some way – they might all be to do with a specific application or perform a specific set of tasks. For example, the Java API is full of packages. One of them is the javax.xml package. It and its subpackages contain all the classes in the Java API to do with handling XML.
Using packages
In a Java source file, the package that this file's class or classes belong to is specified with thepackage
keywordKeyword (computer programming)
In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language....
. This keyword is usually the first keyword in the source file.
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. The following declarationimports all classes from the
java.awt.event
package, while the next declarationimports only the
ActionEvent
class from the package. After either of these import declarations, the ActionEvent
class can be referenced using its simple class name:Classes can also be used directly without an import declaration by using the fully qualified name of the class. For example,
does not require a preceding import declaration.
Note that if you do not use a package declaration, your class ends up in an unnamed package.
Package access protection
Classes within a package can access classes and members declared with default access and class members declared with theprotected
access modifier. Default access is enforced when neither the public
, protected
nor private
access modifier is specified in the declaration. By contrast, classes in other packages cannot access classes and members declared with default access. 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.Creation of JAR files
JAR Files are created with the jar command-line utility. The commandjar 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 patternPattern
A pattern, from the French patron, is a type of theme of recurring events or objects, sometimes referred to as elements of a set of objects.These elements repeat in a predictable manner...
, with levels in the hierarchy separated by periods (
.
) . 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. Package names should be all lowercase characters whenever possible.
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.
Core packages in Java SE 6
— basic language functionality and fundamental types | |
— collection data structure Data structure In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks... classes |
|
— file operations | |
— multiprecision arithmetics | |
— the New I/O New I/O New I/O, usually called NIO, is a collection of Java programming language APIs that offer features for intensive I/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I/O. NIO was developed under the Java Community Process as JSR... framework for Java |
|
— networking operations, sockets, DNS lookups, ... | |
— key generation, encryption and decryption | |
— Java Database Connectivity Java Database Connectivity Java DataBase Connectivity, commonly referred to as JDBC, is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases... (JDBC) to access databases |
|
— basic hierarchy of packages for native GUI components | |
— hierarchy of packages for platform-independent rich GUI Gui Gui or guee is a generic term to refer to grilled dishes in Korean cuisine. These most commonly have meat or fish as their primary ingredient, but may in some cases also comprise grilled vegetables or other vegetarian ingredients. The term derives from the verb, "gupda" in Korean, which literally... components |
|
— classes for creating an applet |
The package is available without the use of an import statement.
External links
- Java Language Specification, 3rd Edition, Chapter 7: Packages
- Code Conventions for the Java Programming Language, Chapter 9: Naming conventions (mentions packages)
- Sun's "Java Tutorials" Lesson: Packages