Static Control Flow ระบุสมาชิกสแตติก รันบล็อกสแตติก และจากนั้นรันเมธอด 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' บล็อกสแตติกอื่นจะพิมพ์ตัวแปรสแตติกที่กำหนดไว้ก่อนหน้านี้และเรียกใช้ฟังก์ชัน 'พิมพ์' อีกครั้ง มีการกำหนดฟังก์ชัน 'พิมพ์' แบบคงที่อื่น ๆ ซึ่งพิมพ์ตัวแปรอื่น มีการกำหนดบล็อกสแตติกอื่นที่พิมพ์ข้อความที่เกี่ยวข้อง นอกบล็อกสแตติกของโค้ดเหล่านี้ มีการกำหนดจำนวนเต็มสแตติกอื่น