Location of Files

Many compilers and JVMs expect source or class files to be in a directory
hierarchy that maps to package name.

Many JVM implementations search for classes along a CLASSPATH.

To find com.artima.jvmsim.JVMSimulator given the
Windows CLASSPATH:

C:\IBMJavaSpeech\lib\ibmjs.jar;C:\mylib;.

The VM would look for com\artima\jvmsim\JVMSimulator.class in:

<INSTALL-DIR>\jre\lib\rt.jar (Runtime Libraries)

<INSTALL-DIR>\jre\lib\ext\*.jar (Standard Extensions)

C:\IBMJavaSpeech\lib\ibmjs.jar

C:\mylib directory

. directory

Private Access

Public Access

Package Access

Protected Access

True Meaning of Private and Protected

The private keyword grants access not to an object, but to a
class. (Accounts can access each other's private
balances.)

Protected access looks like package access to members of the same package.

Outside the package, must have a reference to same class or subclass to
access the object's protected instance members.

Dog can access getLegCount() on a Dog
(including this),
CockerSpaniel, or Poodle
reference, but not an
Animal (other than super) or Cat
reference.

Any subclass can access protected static members of any other subclass.

Exercises

Problem 1.

Select or create a directory of any name on your disk. I will call this
directory your working directory. In the working directory, create
the following file named Cat.java:

// In file Cat.java
class Cat {
private Mouse mouse = new Mouse();
}

Then create a subdirectory in the working directory named "hole"
and place the following file named Mouse.java
in the hole directory:

// In file Mouse.java
package hole;
public class Mouse {
}

At this point, if your working directory happened to be named
C:\goodstuff, then you would have two files:
C:\goodstuff\Cat.java and
C:\goodstuff\hole\Mouse.java.

Finally, change directory to your working directory and compile
Cat.java. The compiler should fail with an error
message. Your task is to get Cat.java to compile by adding
one statement to the Cat.java file. You can't change
Mouse.java nor move any any files around.

Problem 2.

In the PackagesAccess/examples/ex1/com/artima/somelib directory
of the example source code, edit SplitNameReturnVal.java.
Make one small
change to this file: change the access level on the class
(SplitNameReturnVal) from public to package access.

Change to the PackagesAccess/examples/ex1 directory and
attempt to recompile Test.java and Test2.java.
These should now fail compilation because
SplitNamReturnVal is no longer accessible outside the
com.artima.somelib package. Your mission is to move the
two test programs to the com.artima.somelib package and
get them to compile and run there. This will involve editing the
Test*.java files, and moving them to a different directory.
Make no other changes to SplitNameReturnVal.java
other than changing the access level from public to package.

Problem 3.

Problem 4.

Once you get that Problem 3 working, implement the
splitName()
method so it actually parses out the title, first, last, and middle names.
Create a test program named Test3 that accepts one to
four command line arguments. It concatenates these one to four
command line arguments, separating each by a space character, into
a string that is passed to splitName(). Print out
the name after it has been split.