100% found this document useful (1 vote)
98 views

Java Package: From Wikipedia, The Free Encyclopedia

Java packages can be stored in compressed files called JAR files. A package provides a unique namespace for the types it contains. Classes in a package can access each other's package-access members.

Uploaded by

neutralspam5515
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
98 views

Java Package: From Wikipedia, The Free Encyclopedia

Java packages can be stored in compressed files called JAR files. A package provides a unique namespace for the types it contains. Classes in a package can access each other's package-access members.

Uploaded by

neutralspam5515
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java package - Wikipedia, the free encyclopedia

http://en.wikipedia.org/wiki/Java_package

Java package
From Wikipedia, the free 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 les called JAR les, 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.

Contents
1 2 3 4 5 6 7 8 Overview Using packages Package access protection Creation of JAR les Package naming conventions Core packages in Java SE 6 References External links

Overview
In general, a package can contain the following kinds of types. Classes Interfaces Enumerated types

Using packages
In a Java source le, the package that this le's class or classes belong to is specied with the package keyword. This keyword is usually the rst keyword in source le.
[1]

package java.awt.event;

1 de 5

12/08/11 18:53

Java package - Wikipedia, the free encyclopedia

http://en.wikipedia.org/wiki/Java_package

To use a package's classes inside a Java source le, it is convenient to import the classes from the package with an import declaration. 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 qualied name of the class. For example, java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent does not require a preceding import declaration. Note that if you do not use a package declaration, your class ends up in an [2][3] unnamed package.

Package access protection


Classes within a package can access classes and members declared with default access and class members declared with the protected access modier. Default access is enforced when neither the public, protected nor private access modier is specied 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 les


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

2 de 5

12/08/11 18:53

Java package - Wikipedia, the free encyclopedia

http://en.wikipedia.org/wiki/Java_package

jar cf myPackage.jar *.class

compresses all .class les into the JAR le 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 le. The le's name comes next before the contents of the JAR le.

Package naming conventions


Packages are usually dened 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 almost no semantic relationship between packages. The Java Language Specication 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 specic 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 US company named MySoft also creates a fractions package, but names it us.mysoft.fractions, then the classes in these two packages are dened 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 Specication.

Core packages in Java SE 6


Main article: Java Platform, Standard Edition
java.lang (http://download.oracle.com/javase/6/docs /api/java/lang/package-summary.html) java.util (http://download.oracle.com/javase/6/docs /api/java/util/package-summary.html)

basic language functionality and fundamental types collection data structure classes

3 de 5

12/08/11 18:53

Java package - Wikipedia, the free encyclopedia

http://en.wikipedia.org/wiki/Java_package

java.io (http://download.oracle.com/javase/6/docs /api/java/io/package-summary.html) java.math (http://download.oracle.com/javase/6/docs /api//java/math/package-summary.html) java.nio (http://download.oracle.com/javase/6/docs /api/java/nio/package-summary.html) java.net (http://download.oracle.com/javase/6/docs /api/java/net/package-summary.html) java.security (http://download.oracle.com/javase /6/docs/api/java/security/package-summary.html) java.sql (http://download.oracle.com/javase/6/docs /api/java/sql/package-summary.html) java.awt (http://download.oracle.com/javase/6/docs /api/java/awt/package-summary.html) javax.swing (http://download.oracle.com/javase /6/docs/api/javax/swing/package-summary.html) java.applet (http://download.oracle.com/javase /6/docs/api/java/applet/package-summary.html)

le operations multiprecision arithmetics the New I/O framework for Java networking operations, sockets, DNS lookups, ... key generation, encryption and decryption Java Database Connectivity (JDBC) to access databases basic hierarchy of packages for native GUI components hierarchy of packages for platform-independent rich GUI components classes for creating an applet

The java.lang (http://download.oracle.com/javase/6/docs/api/java/lang/package-summary.html) package is available without the use of an import statement.

References
1. ^ http://java.sun.com/docs/books/tutorial/java/package/managingles.html 2. ^ http://java.sun.com/docs/books/tutorial/java/package/createpkgs.html 3. ^ http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#26639

External links
Java SE 6 API Javadocs (http://download.oracle.com/javase/6/docs/api/) Java Language Specication, 3rd Edition, Chapter 7: Packages (http://java.sun.com/docs/books/jls/third_edition/html/packages.html) Code Conventions for the Java Programming Language, Chapter 9: Naming conventions (http://java.sun.com/docs/codeconv /html/CodeConventions.doc8.html) (mentions packages) Sun's "Java Tutorials" Lesson: Packages (http://java.sun.com/docs/books /tutorial/java/package/index.html) Retrieved from "http://en.wikipedia.org/wiki/Java_package" Categories: Java programming language This page was last modied on 28 June 2011 at 15:29.
4 de 5 12/08/11 18:53

Java package - Wikipedia, the free encyclopedia

http://en.wikipedia.org/wiki/Java_package

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of use for details. Wikipedia is a registered trademark of the Wikimedia Foundation, Inc., a non-prot organization.

5 de 5

12/08/11 18:53

You might also like