본문 바로가기
Java 배우기

Initializing Fields 필드 초기화 하기

by 노화방지 Anti-aging Hairstyle 2016. 1. 5.
반응형


선언 안의 필드에 종종 초기 값을 제공할 수 있습니다.
As you have seen, you can often provide an initial value for a field in its declaration:

public class BedAndBreakfast {

    // initialize to 10
    public static int capacity = 10;

    // initialize to false
    private boolean full = false;
}

초기화 값을 사용할 수 있고 초기화를 한 줄에 넣을 수 있을 때 초기화는 잘 작동합니다.
하지만 이런 형태의 초기화는 단순하기 때문에 한계가 있습니다.
초기화가 어떤 논리를 필요로 하는 경우 (예, 오류 처리 또는 복잡한 배열을 채우기 위한 for 루프), 간단한 할당은 적절하지 않습니다.
인스턴스 변수는 오류 처리 또는 다른 로직이 사용될 수 있을 때 생성자에서 초기화 될 수 있습니다.
클래스 변수에 대해 동일한 기능을 제공하기 위하여, 자바 프로그래밍 언어에는 정적 초기화 블록(
static initialization blocks)이 포함되어 있습니다.

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 a 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


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
}

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:

class Whatever {
    public static varType myVar = initializeClassVariable();
        
    private static varType initializeClassVariable() {

        // initialization code goes here
    }
}

The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.


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:

{
    // whatever code is needed for initialization goes here
}

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

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:

class Whatever {
    private varType myVar = initializeInstanceVariable();
        
    protected final varType initializeInstanceVariable() {

        // initialization code goes here
    }
}

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.


반응형

댓글