Java Packages

Group related interfaces and classes together

Purpose: encapsulation like functionality and create a namespace

 

Classes declared with no visibility modifier are package private, i.e, they are not visible outside the package in which they were declared.

The upshot is you could declare a package private class named Cat in package aa and another package private class named Cat in package bb.

 

Package Directive

To make a class part of a package, you must include the following line at the top of the file:

package <package name>;

by strong convention, package names start with lower case characters, so they can be differentiated from class names.

 

Classes that do not contain a package directive are in the ‘default’ package. 

To date all of the classes we have written have been in the default package.

 

Files that are part of a package other than the default package must reside in a subdirectory of the directory holding the files defining classes that are part of the default package.  This subdirectory must have the same name as the identifier that follows keyword package.

 

The compiler gives an error if a class uses the package directive and the <package name> given is not the same as the directory name in which the java file resides.  The compiler also gives an error if the first class declared is in a .java file does not have the same name as the .java file.

 

In eclipse, the ‘src’ directory of each project holds class files in the default package for that project.

You can create a new package using File | New | Package, which creates an appropriately named subdirectory in the src directory.

 

Sub-packages

Any package may have sub-packages and the directory structure matches as described above.

For example, the fully qualified name of the String class is java.lang.String.

There is a package named java with a sub-package named lang, which contains the String class.  At the top of the String class, the package directive reads:

package java.lang;

 

Class A can make a reference to any other class, B, by specifying the fully qualified name of B.

Import directives

Import directives make non-fully qualified class names in packages other than the current package and java.lang visible to the current class.

 

Must appear below the package directive, but above the class signature.

import <packageName>.<ClassName>;   // make a single class visible
or

import <packageName>.*;            // make all classes in a package visible

 

For example:

If I am defining class A, and I would like to refer to class B, which resides in package p, I could say:

p.B anytime I wanted to refer to class B or, at the top of class A, I could say

 

import p.B;

 

then, inside class A, simply type B whenever I wanted to refer to class B.

The wildcard character ‘*’ in the import statement refers only to all classes in a package.  It does not refer to subpackages, e.g.,

import java.awt.*;  // This statement imports only the classes in java.awt, but not in any subpackages of java.awt, e.g., no classes from the java.awt.Event package are imported.

 

Demo

Download demos/packageDemo.zip

Unzip this file into <your eclipse ‘Demos’ project>/src directory

Refresh your demos project