Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
You can provide an initial value for a class or an instance member variable in its declaration: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.public class BedAndBreakfast { public static final int MAX_CAPACITY = 10; //initialize to 10 private boolean full = false; //initialize to false }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.
- The initialization must be expressed in an assignment statement. For example, you can't use an
if
-else
statement.- The initialization expression cannot call any method that is declared to throw a nonruntime (checked) exception. If the initialization expression calls a method that throws a runtime exception, such as
NullPointerException
, it cannot do error recovery. See the chapter Handling Errors with Exceptions for details.
Here's an example of a static initialization block:A static initialization block begins with theimport java.util.ResourceBundle; class Errors { static ResourceBundle errorStrings; static { try { errorStrings = ResourceBundle.getBundle("ErrorStrings"); } catch (java.util.MissingResourceException e) { //error recovery code here } } }static
keyword and is a normal block of code enclosed in braces:{
and}
. TheerrorStrings
resource bundle must be initialized in a static initialization block because thegetBundle
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.
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 theerrorStrings
bundle in the previous example were an instance variable rather than a class variable, you'd move the code that initializeserrorStrings
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 } } }
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.