Previous Table of Contents Next


14.3.4.4. A Mystery Explained

Now that we understand class variables, instance variables, class methods, and instance methods, we are in a position to explore that mysterious method call we saw in our very first Java “Hello World” example:

   System.out.println(“Hello world!”);

One hypothesis is that println() is a class method in a class named out, which is in a package named System. Syntactically, this is perfectly reasonable (except perhaps that class names always seem to be capitalized by convention, and out isn’t capitalized). But if you look at the API documentation, you’ll find that System is not a package name; it is the name of a class (which is in the java.lang package, by the way). Can you figure it out?

Here’s the story: System is a class. It has a class variable named out. out refers to an object of type PrintStream. The object System.out has an instance method named println(). Mystery solved!

14.3.4.5. Static Initializers

Both class and instance variables can have initializers attached to their declarations. For example:

   static int num_circles = 0;
   float r = 1.0;

Class variables are initialized when the class is first loaded. Instance variables are initialized when an object is created.

Sometimes we need more complex initialization than is possible with these simple variable initializers. For instance variables, there are constructor methods, which are run when a new instance of the class is created. Java also allows you to write an initialization method for class variables. Such a method is called a static initializer.

The syntax of static initializers gets kind of bizarre. Consider that a static initializer is invoked automatically by the system when the class is loaded. Thus, there are no meaningful arguments that can be passed to it (unlike the arguments we can pass to a constructor method when creating a new instance). There is also no value to return. So a static initializer has no arguments and no return value. Furthermore, it is not really necessary to give it a name, since the system calls the method automatically for us. What part of a method declaration is left? Just the static keyword and the curly brackets!

Example 14.12 shows a class declaration with a static initializer. Notice that the class contains a regular static variable initializer of the kind we’ve seen before, and also a static initializer—an arbitrary block of code between { and }.

EXAMPLE 14.12. A static initializer.

   // We can draw the outline of a circle using trigonometric functions.
   // Trigonometry is slow though, so we pre-compute a bunch of values.
   public class Circle {
       // Here are our static lookup tables, and their own initializers.
       static private double sines[] = new double[1000];
       static private double cosines[] = new double[1000];

       // Here’s a static initializer “method” that fills them in.
       // Notice the lack of any method declaration!
       static {
           double x, delta_x;
           int i;
           delta_x = (Circle.PI/2)/(1000-1);
           for(i = 0, x = 0.0; i < 1000; i++, x += delta_x) {
               sines[i] = Math.sin(x);
               cosines[i] = Math.cos(x);
           }
       }
           .
           . // The rest of the class omitted.
           .
   }

The syntax gets even a little stranger than this. Java allows any number of static initializer blocks of code to appear within a class definition. What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded.

One common use of static initializers is for classes that implement native methods—i.e., methods written in C. The static initializer for such a class should call System.load() or System.loadLibrary() to read in the native library that implements these native methods.

14.3.4.6. Instance Initializers

In Java 1.1, a class definition may also include instance initializers. These look like static initializers, but without the static keyword. An instance initializer is like a constructor: It runs when an instance of the class is created.

14.3.5. Object Destruction

Now that we’ve seen how you can create and use objects, the next obvious question, a question that C and C++ programmers have been itching to have answered, is how do you destroy objects when they are no longer needed?

The answer is: You don’t! Objects are not passed to any free() method, as allocated memory in C is. And there is no delete operator as there is in C++. Java takes care of object destruction for you, and lets you concentrate on other, more important things, like the algorithm you’re working on.


Previous Table of Contents Next