Java package

Java package

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 statement at the top of a Java source file which is common among a set of source files places a common namespace over those files.

* A package provides a unique namespace for the types it contains.
* Classes in the same package can access each other's protected members.
* A package can contain the following kinds of types.
** Classes
** Interfaces
** Enumerated types
** Annotations

Using packages

In Java source files, the package that the file belongs to is specified with the package keyword.package java.awt.event;To use a package inside a Java source file, it is convenient to import the classes from the package with an import statement. The statementimport java.awt.event.*;imports all classes from the java.awt.event package, whileimport java.awt.event.ActionEvent;imports only the ActionEvent class from the package. After either of these import statements, the ActionEvent class can be referenced using its simple class name:ActionEvent myEvent = new ActionEvent();Classes can also be used directly without an import statement 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 statement.

Package access protection

Classes within a package can access classes and members declared with "default access" and class members declared with the "protected" 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 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 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 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 no semantic relationship between packages. The [http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html Java Language Specification] establishes package naming conventions in order 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 their 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 US company named MySoft also creates a fractions package, but names it us.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 [http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.7 section 7.7] of the Java Language Specification.

Core packages in Java SE 6

java.applet Classes for creating and Implementing appletsjava.net Classes for networking. They include classes for communicating with local computers as well as Internet servers.

External links

*
* [http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.7 Java Language Specification, 3rd edition: Unique Package Names]
* [http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html Java Package Naming Conventions]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Java package — …   Википедия

  • Java Database Connectivity — (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. The Java 2 Platform, Standard… …   Wikipedia

  • Package (Java) — Java package (пакет Java)  механизм, позволяющий организовать Java классы в пространства имен аналогично модулям в языке программирования Модула. Java пакеты могут содержаться в сжатом виде в JAR файлах. Обычно в пакеты объединяют классы… …   Википедия

  • Java Platform, Micro Edition — Java editions Java Card Micro Edition (ME) Standard Edition (SE) Enterprise Edition (EE) …   Wikipedia

  • Package — can refer to:*Packaging and labelling *Mail item larger than a letter *Software package, in computing, a type of file format where software programs and installation material is grouped together **Java package, a concept programming where related …   Wikipedia

  • Java syntax — The syntax of the Java programming language is a set of rules that defines how a Java program is written and interpreted. Data structuresAlthough the language has special syntax for them, arrays and strings are not primitive types: they are… …   Wikipedia

  • Java API for XML Processing — The Java API for XML Processing, or JAXP (pronounced jaks p ), is one of the Java XML programming APIs. It provides the capability of validating and parsing XML documents. The three basic parsing interfaces are: * the Document Object Model… …   Wikipedia

  • Java-Syntax — Duke, das Java Maskottchen Die Syntax der Programmiersprache Java ist in der Java Language Specification definiert, ebenso wie die Semantik von Java. Dieser Artikel gibt einen Überblick über die Java Syntax und stellt einige ihrer Besonderheiten… …   Deutsch Wikipedia

  • Java Servlet — The Java Servlet API allows a software developer to add dynamic content to a Web server using the Java platform. The generated content is commonly HTML, but may be other data such as XML. Servlets are the Java counterpart to non Java dynamic Web… …   Wikipedia

  • Java keywords — The following are brief definitions of the keywords for the Java programming language. Most IDEs use syntax highlighting to display keywords in a different color for easy identification.;abstract:Used in a class declaration to specify that a… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”