Packages and Access Specifiers


(来源:http://www.artima.com)

Objects and Java Seminar by Bill Venners
Lecture Handout

Agenda

  • Describe four ways to think about packages
  • Talk about compiling Java source files
  • Look at the four access specifiers

Packages Partition the Name Space

  • Names of class members (fields, methods, inner classes) need only be unique within the class in which they are declared.
  • Class names must be unique within the package in which they are declared.
  • Package Name + '.' + Simple Name = Fully Qualified Name
    java.lang + '.' + String = java.lang.String
    
  • All fully qualified names used in (a single name space of) a Java application must be unique.

Hierachical Organization

  • Packages enable you to organize your program into logically related groups
  • Package organization is independent of object-oriented organization. (Can subclass a class from another package, etc.)
  • Can't grant special access privileges between a package and a "sub-package."
  • Many compilers and JVMs expect source or class files to be in a directory hierarchy that maps to package name.

Packages are Libraries

  • The package is the library unit in Java.
  • Java APIs are packages: java.lang, java.util, java.io, java.awt, java.applet, and so on.
  • Recommended package naming convention helps combat name conflicts on the internet and elsewhere:

    reversed domain name + '.' + package name

    com.artima.jvmsim

Packages Enable Implementation Hiding

  • The package keyword indicates the package to which the classes in a source file belong:

    package com.artima.jvmsim;
    
  • Classes in source files with no package statement are in the "unnamed" package.
  • Classes within a package can have public or package access.
  • Public classes accessible to anyone. Package-access classes only accessible to package members.

Java Compilation Units

  • Single .java file can have only one public class, and the file name must be PublicClassName.java:

     1 // In source file CoffeeCup.java
     2 package com.artima.vcafe.dishes;
     3 
     4 public class CoffeeCup {
     5     //...
     6 }
     7 
     8 class TeaCup {
     9     //...
    10 }
    
  • Each class in a .java file gets compiled into its own .class file, named ClassName.class.

    $ javac CoffeeCup.java
    $ ls
    CoffeeCup.class CoffeeCup.java TeaCup.class
    

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:
    1. <INSTALL-DIR>\jre\lib\rt.jar (Runtime Libraries)
    2. <INSTALL-DIR>\jre\lib\ext\*.jar (Standard Extensions)
    3. C:\IBMJavaSpeech\lib\ibmjs.jar
    4. C:\mylib directory
    5. . 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.

Create a Test3 class in the PackagesAccess/examples/ex1 directory, whose main() method invokes Test2's main() method.

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.