The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

Initializing Instance and Class Members

You can provide an initial value for a class or an instance member variable in its declaration:
public class BedAndBreakfast {
    public static final int MAX_CAPACITY = 10;
    //initialize to 10
    private boolean full = false;
    //initialize to false
}
This works well for member variables of primitive data types. Sometimes, it even works when creating arrays and objects. But this form of initialization has limitations. If these limitations prevent you from initializing a member variable in its declaration, you have to put the initialization code elsewhere. To initialize a class member variable, put the initialization code in a static initialization block, as the following section shows. To initialize an instance member variable, put the initialization code in a constructor.

Using Static Initialization Blocks

Here's an example of a static initialization block:
import java.util.ResourceBundle;
class Errors {
    static ResourceBundle errorStrings;
    static {
        try {
            errorStrings = 
               ResourceBundle.getBundle("ErrorStrings");
        } catch (java.util.MissingResourceException e) {
            //error recovery code here
        }
    }
}
A static initialization block begins with the static keyword and is a normal block of code enclosed in braces: { and }. The errorStrings resource bundle must be initialized in a static initialization block because the getBundle method can throw an exception if the bundle cannot be found. The code should perform error recovery. Also, errorStrings is a class member, so it should not be initialized in a constructor.

A class can have any number of static initialization blocks that appear anywhere in the class body. The runtime system guarantees that static initialization blocks and static initializers are called in the order (left to right, top to bottom) that they appear in the source code.

Initializing Instance Members

If you want to initialize an instance variable and cannot do it in the variable declaration for the reasons cited previously, put the initialization in the constructor(s) for the class. If the errorStrings bundle in the previous example were an instance variable rather than a class variable, you'd move the code that initializes errorStrings to a constructor for the class, as follows:
import java.util.ResourceBundle;
import java.util.ResourceBundle;
class Errors {
    ResourceBundle errorStrings;
    Errors() {
        try {
            errorStrings =
              ResourceBundle.getBundle("ErrorStrings");
        } catch (java.util.MissingResourceException e) {
            //error recovery code here
        }
    }
}

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.