|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 5
The <init> method is not actually part of the Java language. Rather, it is something the Java virtual machine expects to see in a Java
class file. This distinction is significant because the Java language does not depend on the class file. Java source can be
compiled into other binary formats, including native executables. A Java compiler that translates Java language source into
some other binary format need not generate a method named <init>, so long as objects are initialized in the proper way at the proper time. The Java Language Specification (JLS) details the
order of initialization and when it occurs, but doesn't say how it is actually accomplished. (See the Resources section for information on the Java Language Specification.) Still, understanding how initialization works inside class files can
help you understand the order of initialization in the language.
Besides providing constructors, Java offers one other way for you to assign an initial value to instance variables: initializers. As mentioned previously, the two kinds of initializers in Java are instance variable initializers and instance initializers.
Instance variable initializers
In a constructor, you have the freedom to write as much code as needed to calculate an initial value. In an instance variable
initializer, you have only an equals sign and one expression. For example, if you wanted to always start coffee cups out with
355 milliliters of fresh brewed coffee in them, you could initialize innerCoffee with a constructor:
// In source packet in file init/ex9/CoffeeCup.java
class CoffeeCup {
private int innerCoffee;
public CoffeeCup() {
innerCoffee = 355;
}
// ...
}
Alternatively, you could initialize innerCoffee with an instance variable initializer:
// In source packet in file init/ex10/CoffeeCup.java
class CoffeeCup {
private int innerCoffee = 355; // "= 355" is an initializer
// no constructor here
// ...
}
The right-hand side of the equals sign in an initializer can be any expression that evaluates to the type of the instance variable.
Instance initializers
Java 1.1 introduced the instance initializer, which is also called the instance initialization block. Here is the same CoffeeCup class with its innerCoffee variable initialized by an instance initializer:
// In source packet in file init/ex19/CoffeeCup.java
class CoffeeCup {
private int innerCoffee;
// The following block is an instance initializer
{
innerCoffee = 355;
}
// no constructor here
// ...
}
This manner of initializing innerCoffee yields the same result as the previous two examples: innerCoffee is initialized to 355.
Instance initializers are a useful alternative to instance variable initializers whenever: (1) initializer code must catch exceptions, or (2) perform fancy calculations that can't be expressed with an instance variable initializer. You could, of course, always write such code in constructors. But in a class that had multiple constructors, you would have to repeat the code in each constructor. With an instance initializer, you can just write the code once, and it will be executed no matter what constructor is used to create the object. Instance initializers are also useful in anonymous inner classes, which can't declare any constructors at all.