Introduction to Java Package
Package is a mechanism for organizing a group of related
files in the same directory. In a computer system, we organize files into
different directories according to their functionality, usability and category.
An example of package is the JDK package of SUN Java as shown below:
In Java, a package is a group of related types classes and interfaces which provides access protection and name space management to use those classes and interfaces. Apart from these, Java also supports special kinds of classes and interfaces known as Enumerations and Annotation, respectively.
Packaging is used to organize classes that belong
to the same files or providing similar functionality. Apart from this, it
also helps the programmer to avoid class name collision when we use the same class name as
available in the another packages. For example, the
class of j
package is used to access the methods of the Date class. But, if we have a
class name "
Date" (user defined) in our
program, its name would crash with the
Date class of j
package. To avoid this problem, we can use Package through which the
"Date" class can be put up into another package like india.mycompany.Date
without collision with anyone.
Generally, classes in one package (or directory) have different functionality from the another package. For example, classes in java.io package manage the Input/Output streams to read and write data from and to files, while classes in java.net package give us the way to deal with the network operations.
Features of a Java package
Naming convention (Rules) for using the packages
Package names are written in all lowercase to avoid conflict with the names of classes or interfaces.
The directory name must be same as the name of package that is created using "package" keyword in the source file.
Before running a program, the class path must be picked up till the main directory (or package) that is used in the program.
If we are not including any package in our java source file then the source file automatically goes to the default package.
In general, we start a package name begins with the order from top to bottom level.
In case of the internet domain, the name of the domain is treated in reverse (prefix) order.
Java Package Names and Directory StructureJava Packages are usually defined using a hierarchical naming pattern in which the Package name consists of words separated by periods. The first part of the Package name represents the main directory in which other subpackages or classes are put up. The remaining part of the Package name reflect the sub contents of the package. Just see the example of packages names:
|Internet Domain||Package name||Subdirectory path|
Lets see a general example to understand the directory
structure of the package importing the javax.swing package.
Output of the program:
In the above example, a Java Package "javax.swing" is a buid-in package imported from the JFC (Java Foundation Classes). The first name of that package "javax" represents the main package. The second part of the package name "swing" stands for the contents of the package. All classes and interfaces reside in the swing package can be accessed according to their functionality.
How to Set the Class Path:
The classpath is a user defined environment variable that is used by Java to determine where predefined classes are located. It tells the java tools and applications where to find user-defined classes. The syntax to set the classpath is shown as:
You can set multiple path entries that are separated by semi-colons.
To make this thing more understandable, let's put the "HelloWorld" class along with its package "mypackage" under the "C:\mainpackage" directory. Thus the directory structure for this package is shown as:
Now we have changed the location of the package from
C:\mypackage\HelloWorld.java to C:\mainpackage\mypackage\HelloWorld.java. Then
CLASSPATH needs to be changed to point the new location
of the package mypackage accordingly shown as:
set CLASSPATH = .;C:\mainpackage;
Although in such condition, Java will look for java
classes from the current directory instead of the "C:\mainpackage" directory.
Access protection in packages:
|No modifier (default)|| The classes and members specified in the same package are accessible to
all the classes inside the same package.
|public||The classes, methods and member variables under this specifier can be accessed from anywhere.|
|protected|| The classes, methods and member variables under this modifier are accessible by all subclasses,
and accessible by code in same package.
|private||The methods and member variables are accessible only inside the class.|
Access to fields in Java at a Glance:
|The class itself||Yes||Yes||Yes||Yes|
|A subclass in same package||Yes||Yes||Yes||No|
|Non sub-class in the same package||Yes||Yes||Yes||No|
|A subclass in other package||Yes||Yes||No||No|
|Non subclass in other package||Yes||No||No||No|
Recommend the tutorial