There are several kinds of variables:
- Member variables in a class?these are called fields.
- Variables in a method or block of code?these are called local variables.
- Variables in method declarations?these are called parameters.
Initializing Fields
This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity. If initialization requires some logic (for example, error handling or apublic class BedAndBreakfast { public static int capacity = 10; //initialize to 10 private boolean full = false; //initialize to false }
for
loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.
Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.
Static Initialization Blocks
A static initialization block is a normal block of code enclosed in braces,{ }
, and preceded by the static
keyword. Here is an example:static { // whatever code is needed for initialization goes here }
출처 : http://download.oracle.com/javase/tutorial/java/javaOO/variables.html
A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.There is an alternative to static blocks ?you can write a private static method:
The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.class Whatever { public static varType myVar = initializeClassVariable(); private static varType initializeClassVariable() { //initialization code goes here } }
Initializing Instance Members
Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods.Initializer blocks for instance variables look just like static initializer blocks, but without the
static
keyword:The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.{ // whatever code is needed for initialization goes here }
A final method cannot be overridden in a subclass. This is discussed in the lesson on interfaces and inheritance. Here is an example of using a final method for initializing an instance variable:
This is especially useful if subclasses might want to reuse the initialization method. The method is final because calling non-final methods during instance initialization can cause problems. Joshua Bloch describes this in more detail in Effective Java.class Whatever { private varType myVar = initializeInstanceVariable(); protected final varType initializeInstanceVariable() { //initialization code goes here } }
출처 : http://docstore.mik.ua/orelly/java-ent/jnut/ch03_02.htm
A variable declaration is a statement that appears within a Java method; the variable initialization is performed when the statement is executed. Field declarations, however, are not part of any method, so they cannot be executed as statements are. Instead, the Java compiler generates instance-field initialization code automatically and puts it in the constructor or constructors for the class. The initialization code is inserted into a constructor in the order it appears in the source code, which means that a field initializer can use the initial values of fields declared before it. Consider the following code excerpt, which shows a constructor and two instance fields of a hypothetical class:
In this case, the code generated for the constructor is actually equivalent to the following:public class TestClass { ... public int len = 10; public int[] table = new int[len]; public TestClass() { for(int i = 0; i < len; i++) table[i] = i; } // Rest of the class is omitted... }
If a constructor begins with a this() call to another constructor, the field initialization code does not appear in the first constructor. Instead, the initialization is handled in the constructor invoked by the this() call.public TestClass() { len = 10; table = new int[len]; for(int i = 0; i < len; i++) table[i] = i; }
댓글 없음:
댓글 쓰기