Using Packages

Users of JBuilder are aware of the statement package whatever; that is placed as the first statement of a file. One class can directly access instance variables on another class if the two classes are in the same package. You will have to use the dot convention to do this:

file:A.java
class A
{
   int i ;

   void makeEqual(B b)
   {
      b.j = i ;
   }

}

file:B.java
{
   int j ;

   void makeEqual(A a)
   {
      a.i = j ;
   }
}

If a variable is marked private or protected, than this sharing cannot take place. If the variable is marked public, then this sharing can take place even between two classes not in the same package.

Two things are needed to place a class in a package. There is a language thing: put package package_name; as the first statement in the .java file, and a compiler thing, place all classes in package x in a directory called x.

To compile, go to the parent directory of x, and run:

  javac x/filename.java
To run a class in filename.class in package x, in the same parent directory of x, run:
  java x.filename

For example: this directory has two subdirectories, hashing and scanwww. In the hashing subdirectory are classes in the hashing package. In the scanwww subdirectory are classes in the scanwww package.

To compile, in this directory I use:

  javac hashing/HashingTest.java
  javac scanww/ScanWww.java
and to run:
  java hashing.HashingTest
  java scanww.ScanWww