Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Lab 10 Java

...

Packages, Generics; 

Java

...

Packages

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 funList , you must add the declaration package funList ; to the very top of the class source file.

...

Now, remove the public access from the EmptyIntList class. By default, a class is "package-private", i.e., it is known within the package, but not from outside.  Try to compile again.  You should see a few error messages saying that you can't use EmptyIntList.java because it is not public. This is because the TestEmptyIntList class is not part of the funList package. One way to resolve this problem by making TestEmptyIntList part of the funList 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 no class can be part of more than one package. For now, just make EmptyIntList.java public again, and recompile TestEmptyIntList.java . You should get no error. Try to run Test_List.java now by click the Test button in DrJava.

...

Java Generics

DrJava Projects
Here's some more information on using DrJava's project capabiltiies:
DrJavaprovides a utility called Project as a way to manage large Java programs with many files and many packages. We will illustrate the use of DrJavaProject by writing a few visitors for the funlist package.

...