Java 靜態控制流
靜態控制流標識靜態成員,執行靜態塊,然後執行靜態 main 方法。我們來看一個示例 −
示例
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 的類包含一個靜態變數和一個 main 函式,其中呼叫了“print”函式。另一個靜態塊列印前面定義的靜態變數,並再次呼叫“print”函式。定義了另一個靜態“print”函式,用於列印另一個變數。還定義了另一個靜態塊,用於列印相關訊息。在所有這些程式碼的靜態塊之外,定義了另一個靜態整數。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP