Packages in Java


General Information



Overview

Many implementations of Java use a hierachical file system to manage source and class files.
import
Import
package
Packages

Access specifiers

Notice that private protected is not syntactically legal.
Summary of access to fields in Java
access byprivate"package"protectedpublic
the class itselfyesyesyesyes
a subclass in same packagenoyesyesyes
non-subclass in same packagenoyesyesyes
a subclass in other packagenonoyesyes
non-subclass in other packagenononoyes

Example

javac one/Main.java another/Unrelated.java  
java one.Main

Classes in the same package can be in different directories.

dir1/dir2/A/B/C.class
dir3/A/B/D.class
java -classpath dir1/dir2:dir3 ...

Packages are simple-minded in Java and dangerous. You can accidentally use the wrong classes. So if you misspell a class name and there happens to be a class by that name in the same directory, Java will not detect it.

import java.util.*;  // imports java.util.Date
import java.sql.*;   // also import java.util.Date
public class Collision {
   HashMap m;
   Date d;
}
Does not compile.
Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Wed Sep 25 17:08:55 EDT 2002