Sunday, May 2, 2010

What is a Package & Define it?

A Java package is a mechanism for organizing Java classes into namespaces similar to the modules of Module. 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 others package-access members.

Define a Package.
  • Package pkg;[here pkg is the name of package ]
Define multilevel Package 
  • Package pkg1[.pkg2[.pkg3[.pkg4]]];   
Package Declaration

This keyword is usually the first keyword in source file.

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 statement.
import java.awt.event.*;

imports all classes from the java.awt.event package, while the next statement
import java.awt.event.ActionEvent;

imports only the ActionEvent class from the package.
ActionEvent myEvent = new ActionEvent();

Classes can also be used directly without an import statement by using the fully-qualified name of the class.
java.awt.event.ActionEvent myEvent = new java.awt.event.ActionEvent();

No comments:

Post a Comment