정적 제어 흐름은 정적 멤버를 식별하고 정적 블록을 실행한 다음 staticmain 메서드를 실행합니다. 예를 들어 보겠습니다 -
예시
public class Demo{
static int a = 97;
public static void main(String[] args){
print();
System.out.println("The main method has completed executing");
}
static{
System.out.println(a);
print();
System.out.println("We are inside the first static block");
}
public static void print(){
System.out.println(b);
}
static{
System.out.println("We are inside the second static block");
}
static int b = 899;
} 출력
97 0 We are inside the first static block We are inside the second static block 899 The main method has completed executing
Demo라는 클래스에는 정적 변수와 'print' 함수가 호출되는 주 함수가 포함되어 있습니다. 또 다른 정적 블록은 이전에 정의된 정적 변수를 인쇄하고 '인쇄' 함수를 다시 호출합니다. 다른 변수를 인쇄하는 또 다른 정적 '인쇄' 기능이 정의되어 있습니다. 관련 메시지를 인쇄하는 또 다른 정적 블록이 정의됩니다. 이러한 모든 정적 코드 블록 외부에 또 다른 정적 정수가 정의됩니다.