Programmers bundle groups of related classes and
interfaces into packages to:
Definition: A package is a collection of related classes and interfaces providing access protection and namespace management.The classes and interfaces that are part of the Java platform are members of various packages that bundle classes by function:
- fundamental classes are in
java.lang
,
- classes for reading and writing (input and output) are in
java.io
,
- and so on.
Draggable
,
that classes implement if they can be dragged with the mouse by the
user: //in the Graphic.java file
public abstract class Graphic {
. . .
}
//in the Circle.java file
public class Circle extends Graphic implements Draggable {
. . .
}
//in the Rectangle.java file
public class Rectangle extends Graphic implements Draggable {
. . .
}
//in the Draggable.java file
public interface Draggable {
. . .
}You should bundle these classes and the interface in a package for several reasons:
- You and other programmers can:
- easily determine that these classes and interfaces are related
- know where to find classes and interfaces that provide graphics-related functions.
- The names of your classes wont conflict with class names in other packages, because the package creates a new namespace.
- Classes within the package can have (package) access to one another yet restrict access from classes outside the package.
package
statement at the top of the source
file in which the class or the interface is defined. Circle.java
and puts the Circle
class in the graphics
package: package graphics;
public class Circle extends Graphic implements Draggable {
. . .
}
Circle
class is a public member of the graphics
package. package
statement at the top of
every source file that defines a class or an interface that is to be a
member of the graphics
package. Rectangle.java
and so on: package graphics;
public class Rectangle extends Graphic implements Draggable {
. . .
}
package
statement is the entire
source file: Circle.java
and Rectangle.java
are also members of the graphics
package. package
statement, your class
or interface ends up in the default package, which is a package
that has no name. Rectangle
class when
there is already a Rectangle
class in the java.awt
package. Rectangle
class in the graphics
package is graphics.Rectangle
Rectangle
class in the java.awt
package is java.awt.Rectangle
. This works just fine unless 2 independent programmers use the same name for their packages. What prevents this problem? Convention.
com.company.package
.
com.
in this example from their package names.Each is appropriate for different situations, as explained in the following sections.
- Refer to the member by its long (qualified) name
- Import the package member
- Import the members entire package
Rectangle
class declared in the graphics
package in the previous
example: You could use this long name to create an instance ofgraphics.Rectanglegraphics.Rectangle
:graphics.Rectangle myRect = new graphics.Rectangle();This sometimes is done for 1-time use of a member. For example,
public class FancyObject implements java.io.Serializable { ... }
import
statement at the beginning of your file before any class or interface
definitions but after the package
statement. Circle
class from the graphics
package created in the previous
section: Now you can refer to theimport graphics.Circle;Circle
class by its simple name:Circle myCircle = new Circle();
To import all the classes and interfaces contained in a particular package, use theimport
statement with the asterisk(*)
wildcard character:Now you can refer to any class or interface in theimport graphics.*;graphics
package by its short name:The asterisk in theCircle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();import
statement can be used only to specify all the classes within a package.
It cannot be used to match a subset of the classes in a package. For example, the following does not match all the classes in the
graphics
package that begin withA
:The * is not recursive: It does not import any classes in subpackages.import graphics.A*; // does not work
Java runtime system automatically imports the
java.lang
package.
Rectangle
in the graphics
package. java.awt
package also contains a Rectangle
class. graphics
and java.awt
have been imported, the following is
ambiguous: Rectangle rect;
Rectangle
class
you want:
graphics.Rectangle rect;
.java
. Rectangle
class is in a file named Rectangle.java
, which is in a
directory named graphics
. graphics
directory can be anywhere in the file
system. The figure below illustrates how this.The qualified name of the package member and the path name to the file are parallel, assuming the UNIX file name separator slash (/):
class name graphics.Rectangle
pathname to file graphics/Rectangle.java
taranis.com
precedes all its package names with com.taranis
. graphics
package that contained a Rectangle.java
source file, it is in a directory as shown below..class
, as shown in the
following figure. .java
file, a .class
file
should be in a series of directories that reflect the package name. You thus can give the classes directory to your customoers without [readily] revealing your source code.
Definition: A class path is an ordered list of directories or ZIP files in which to search for class files.
classes
but
not com
or any of the directories below com
.
.class
file with its full package name.package
statement as the first statement in the source file for the class or
the interface. To use a class or an interface that's in a different package, you have 3 choices:
- Use the fully qualified name of the class or the interface.
- Import the class or the interface.
- Import the entire package of which the class or the interface is a member.
In this course, set your class path so the compiler and interpreter can find your source and class files for your classes and interfaces.