結論
注意 只有顯式的加載類 JVM才會加載到記憶體中
- 先加載父類的靜態代碼塊 然后執行子類靜態代碼塊
- 當前類存在類靜態變數
注意參考型別沒進行賦值操作初始化為null 并不會顯式的加載類又存在靜態代碼塊 會先執行前者進行初始化 再執行靜態代碼塊 - 在實體化類的時候 執行順序 構造代碼塊-->構造方法
存在父類先執行父類 - 注意 靜態成員變數/靜態代碼塊只在JVM運行時 類加載到記憶體的時候執行一次
public class Test {
static B b=new B(); //這里存在實體 會加載類B 類A
A a =new A();
static {
System.out.println("test static");
}
public Test(){
System.out.println("test constructor");
}
{
System.out.println("test module");
}
public static void main(String[] args) {
//A1-->B1-->A2-->A3-->B2-->B3-->TEST STATIC
}
}
class A{
static {
System.out.println("A1");
}
{
System.out.println("A2");
}
public A(){
System.out.println("A3");
}
}
class B extends A{
static {
System.out.println("B1");
}
{
System.out.println("B2");
}
public B(){
System.out.println("B3");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/550918.html
標籤:其他
下一篇:返回列表
