![]() |
COMP 202: Principles of Object-Oriented Programming II← Packages → |
Home — Fall 2008 | | | Information | | | Resources | | | Exam Resources | | | OwlSpace | | | COMP 201 |
A Java package is a grouping of classes similar to the notion of a directory
is a grouping of files. Packages are used to help avoid name clashes and to hide
particular pieces of code from the clients. A package has a name, such as
utility
or java.lang.util
. In general, a package name is a
series of strings of alphanumeric characters (starting with an alphabetic
character) and separated by periods. To make a java class part of a particular
package, say scheme
, you must add the declaration package
scheme;
to the very top of the class source file.
Also, you will need to put the file in the directory structure that mirrors
the package name. For example, the java classes that belong to the package
scheme should be in a directory also named scheme
. If you don't do
this, it will still compile, but it won't run correctly.
Download and unzip the following file to use in the exercises: listSource.zip
package scheme;
declaration to the top of
AList.java
. You need to create a subdirectory called scheme
and move AList.java
into it.
Now compile using
javac scheme/AList.javaNote that you should always compile from your project's main directory. If you compile from within a package subdirectory, it doesn't find all the supporting definitions.
We can't run anything yet, because that's just a piece of the whole program.
package scheme;
declaration to the top of
EmptyList.java
and NEList.java
, and move them into the
scheme
subdirectory. Do not make
ListClient.java
part of the package. TestLength.java
does not have a package name, and it thus said to be in the no-name
(or default) package.
Also, remove the public
access from the EmptyList
and NEList
classes. By default, a class is "package-private",
i.e., it is known within the package, but not from outside. If you try to
compile TestLength.java
now, you will get an error message. Try
it to see what happens.
You need to add the statement import scheme.*;
to the top of
TestLength.java
to indicate to the compiler that you are using
all the public classes in that package. Try to compile it
again.
You should see a few error messages saying that you can't use
EmptyList.java
and NEList.java
because these classes are
not public. This is because the TestLength
class is not part of
the list
package. One way to resolve this problem by making
TestLength
part of the list
package. A class of a
package can access all the classes (public or "package-private") in the
package. However this is not a good solution in general because a client may
be using many classes from different packages, but not class can be part of
more than one package. A better solution is to use a "factory". We will
discuss factories in another lab. For now, just make EmptyList.java
and NEList.java
public again, and recompile
TestLength.java
. You should get no error. Try to run
TestLength
now.
URL: http://www.cs.rice.edu/~javaplt/mgricken/teaching/202/08-fall/info/packages.shtml
Copyright © 2008-2010 Mathias Ricken and Stephen Wong