Classes

6.5 Classes

SYNTAX

[public] class [ extends SuperClass] {
.
.
.
variable and method declarations
.
.
.
}

DESCRIPTION

The class is the fundamental unit of code in Java. It is a collection of variables and methods that can be thought of together as a single unit. One thinks of classes as "objects", tangible things that can be created and destroyed.

Every Java program contains at least one class. All but the most simple programs will have more than one class in existence at a time. Indeed, it is common to have many copies of the same class in existence at the same time. Of course a given thread of program flow can be only in one class's method at at time, but it can pass from class to class as a class method calls member methods from another class.

The body of the class contains the method and variable declarations that comprise the class. Method declarations are covered in a separate section. Variable declarations work like those in methods , except that you may prepend them with an access modifier like those used in methods.

The public declaration of a class makes the class available to other classes not in the same source code file. There can only be one public class per file, and it must bear the same name as the file (without the .java extension). You can leave out the public declaration if there is only one class in your program, or if your class will only be used by other classes in the same .java file.

If a class extends another class, it inherits the member methods and variables of this other class, called the super class. This means, basically, that if you have a copy of a class, you also have a copy of the variables and methods of the super class. This is an amazing invention that saves a lot of code writing.

CREATING A CLASS INSTANCE

Let us consider the following class: the Double. The Double contains methods for dealing with primitive double variables. For example, you might want to convert a String to a double or vice versa. The Double class can be thought of as a bloated double - it contains a double value as well as a bunch of methods to operate on this value.

To create a Double, you can use the new command. Consider the following:

     Double val = new Double(3.4);
This line declares the existence of the variable val of type Double. It also assigns to val a copy of the Double class initialized to the value 3.4.

Notice that the new Double declaration takes an argument (in this case it is the value of the Double). A class can contain a method with the same name as the class, called the constructor method. When a copy of the class is created with a new statement, the constructor method will be called along with the correct arguments provided. So the Double class has a method declared in it that looks something this:

public class Double {

     public Double(double value) {
         ... code here ...
     }

     ... code here ...

}

ACCESSING CLASS VARIABLES AND METHODS

Accessing variables and methods withing a class (for example a class method calling a another method of the same class) is easy; you just use the class name. You can see this, for example, in the main method of the Newton class, which calls other class methods f and fprime.

Accessing variables in other classes is not much more difficult. To begin, you should know that there are two kinds of class members: instance and class members. You can use instance variables if you have an actual copy of a class on hand (for example, created using new). Method variables, on the other hand, can be used at any time. Here is an example with the Double class we saw earlier. Now the Double class has an instance method called toString that returns a String representation of a Double's value. So you have to have an actual Double on hand to use this method (how else would it know what value to use?).

On the other hand, the Double class has a method variable called MAX_VALUE that contians the maximum value a Double (and also a double) can contain. You don't need to have a Double on hand to access this variable.

You access instance and method members differently. Method methods are prepended with the name of the class and a period, so in this case, we could use Double.MAX_VALUE. This would give us a double containing the largest possible double value. For instance variables, you must use the name of the instance in place of the class name, , followed by a period. So, if we created val as above with:

     Double val = new Double(3.4);
we could print out its value with:
     System.out.println( val.toString() );
Next 6.6 Methods

David Maxwell, who is still writing this, would like to hear your comments and suggestions.