Classes and Packages

Class Types: (note, inner classes may have different capabilities since they are "members")
Here is an interesting page! A modifier matrix (although I do see some contradictions to other sources)! And then, the Java Language Specification 8.1.1 Class Modifiers

abstract
An abstract class used to require at least one abstract method. (not according to the tutorial)
An abstract class cannot be instantiated, and must be subclassed
final
A final class is declared as the last class of any subclass chain. (efficiency) Classes declared final may not be subclassed.
The Math class is a final class
public
Can be accessed by other classes, either directly or subclassing. The class must be imported to be used by other packages, otherwise it is only available within the package where it is declared
(note: Classes are not private or protected, variables and methods (members) are... except nested class members)

synchronized (early versions of Java - no longer allowed)
This class modifier declared that the methods defined in the class were all synchronized.)

Classes Tutorial

Tutorials also: Packages, Import, Interfaces, Implements

abstract

One of the most useful features of an object-oriented language is the ability to declare a class that defines how to use the class without having to declare the methods. This is useful when the implementation is going to be specific to each user, but each user should use the same methods.

Example: Suppose (just suppose) you wanted to have a bunch of graphics objects. Consider a Shapes Class

When you implement the Rectangle or Circle class, you extend the Shapes class. With Rectangle, you'd provide a concrete implementation of the drawMyself() method that draws a rectangle.Each shape will be specific so each drawMyself() will vary. (polymorphism)

setPosition() method is declared public void since it can be implemented here for all

The class java.awt.Graphics is an abstract class:
see
the API for class description

Methods are declared in the Graphics class, but the actual code to execute the method is done elsewhere (dependent on browser/machine etc.).

When a class contains an abstract method the class must be declared abstract.

However, not all methods in the abstract class need to be abstract... some methods you might want to be inherited by all

Abstract classes should generally not have private methods (why? ) nor can they have static methods. (private methods will compile though)

the SUN tutorial "Abstract Classes and Methods"

Import

Class packages are loaded with the import keyword.

Inner Classes:

Java 1.1 provided some new "types" of classes: nested classes.

Why would you want them? and examples from the tutorial

You have seen these when you compile code and get classes with the $ in the .class name. These are all addressed in Java in a Nutshell, Chapter 3 as well as in the SUN tutorial

More pages of interest are Inner Classes and the tutorial questions (a local copy )

Also, in CoreJava, Vol1, Chapter 6: Interfaces and Inner Classes, "Are inner classes useful?" some excerpts

Packages

Packages in Java are a way of grouping together related classes and interfaces (dot (.) separated)

They

Declaration

If a package statement appears in a Java source file, it must be the first thing in that file (except for comments and white space...)

Then you define your classes. These classes (the ones declared with this same package name) are grouped together. These other classes are usually located in separate source files

Packages can be further organized into a hierarchy:
java.awt.image.ColorModel

Import and Ambiguity

Look into the Date.java file see
package java.util;
at the top. This identifies that it is in that package.
You might note also that there is a java.sql.Date class in the package java.sql

If your source file has no package, java adds the classes in it to its default package (current directory)

The classes in the java package are guaranteed to be available in any Java implementation, and are the only classes guaranteed to be available across different platforms. Classes in other packages (sun, netscape) may be available only for specific implementations.

By default, your Java classes have access to only the classes in java.lang To use classes from any other package, you have to either

Usually it is easier to
import java.util.*;
and then use
Date today = new Date();

If two packages each have classes with the same name, you can't import them both. Try it and see what happens ...

Also, you can only use * to import multiple classes in a package. In fact, normally importing all classes is simpler, and if has no negative effect on compile time or code size, so there is generally no reason not to do it. (Remember that import is not the same as #include in C ... java does dynamic class loading.)

The use of * cannot be used for multiple packages, i.e., import java.* won't work

See CoreVolume 1, Chapter 4: Objects and Classes section on Packages for how the compiler and virtual machine locate packages (CLASSPATH) also the JavaWorld article. Interesting note: "It looks in the source files to see if the source is newer than the class. If so, the source file is recompiled automatically."

Location, Compiling and Calling

Files of a package should be located in a subdirectory that matches the full package name.

For example, for the programmer, the way to keep everything in its proper place is to use Java packages. Suppose you wanted to define a module with say, all of your java beans. Add the following line to the top of your file:

package acme.beans;

For this package, you will now need to create a directory structure (/acme/beans) so the Java compiler can put the generated class files in the proper locations. (The java compiler used to create the directories upon compilation if you had not done so already. These days you need to use the -d option for it to do this (see below)))

Here's how to create the appropriate directory structure for the acme.beans package below the current working directory.

Assuming a package name of "acme.beans" create directories for Java source files and Java class files:

         mkdir -p ./src/acme/beans
         mkdir -p ./classes/acme/beans
In case you are unfamiliar with the -p option (from the man pages):
-p        With this option, mkdir creates dir  by  creating
          all  the  non-existing  parent  directories first.
         

You might also be interested in these options for javac while we are at it. In general, see http://www.ecst.csuchico.edu/javaAPI/tooldocs/ for options on java "tools".

Suppose you wanted to compile a couple of .java files. Oh, this reminds me. If you have a class that needs to instantiate another class (and possibly import), make sure that the class that this one will use has been compiled. Example

Compiling the code to get the correct structure:

        javac -d . NervousText06.java     // if the package name is acme.beans you will see ./acme/beans/NervousText06.class

javac -d . NervousText06BeanInfo.java

OR

For your information:

Scope: Remember, if you do not specify features as either private or public, then that feature (class, method, variable) can be accessed by all methods in the same package. (friendly)

So if a class is not defined as public, then only other classes in the same package can access it. This is often OK at first since you are defining all of your classes in the same directory (by defaults, same package).

For methods, you should generally specify if you want them to be public or private (like setting protocols (accessors, etc) in Smalltalk). Core2 suggests that data (cv and iv) always be private (to promote encapsulation)

Note: every source file can contain, at most, one public class, which must have the same name as the file.

Many books suggest *every* class be defined in a separate file supporting the compiler convention that only one class should be located in each Java source file - actually the compiler cares only about every public class being a separate file

The above is a common cause of compiler error - hard to find if you don't know this convention!

Also see the
tutorial questions page a local copy

O'Reilly course 3: Lessons 3 (packages), 5 (abstract classes), 7 (nested classes)