Learning Java programming ideas (1)-initialization and loading

Class initialization and loading

1. Constructor initialization

For example, the following example

public class Test{
	int i;
	Test(){
	i=7;
	}
}

According to the characteristics of java,
first i = 0. Then i = 7
When loading a class (not to mention the static code block), first initialize its member variables , which are 0 for basic types.
For reference types it is Null. Then initialize them in the constructor.

2. Initialization with static code blocks

The static code block is the first place where a class is loaded or used
(
No need to instantiate, Even if the static methods and data in the calling class are also used) The
java runtime class runs on demand and needs to be loaded again. It will not load if it is not needed.
It is worth noting that Static code blocks or static variables are loaded before the main method
It ’s easy to forget that conventional thinking always regards main as the entry point for the program to run

Below is an example

package com.example.demo;

public class Test {
   static {
       // 1
       System.out.println("我是静态代码块");
   }
   public static void main(String[] args){
       // 5 and 6 and 7
       System.out.print("main方法中");
       new One();
       new Two();
   }
   // 2
   static One one = new One();
   // 3
   static Two two = new Two();
}
class One{
   One(){
       System.out.println("我是静态实例one");
   }
}
class Two{
   Two(){
       // 4
       new One();
       System.out.println("我是静态实例two");
   }
}

operation result

我是静态代码块
我是静态实例one
我是静态实例one
我是静态实例two
main方法中我是静态实例one
我是静态实例one
我是静态实例two

The order of the statements of the running result is reflected in the code

3. Initialize the inheritance of
a direct look at an example

package com.example.demo;

// 运行类第一个检查的是下面这句话
public class Test extends TestFather{ // 向上查testfather
    // 上面这句 查找原始祖先,没有重写Object 所以祖先就是GrandFa
    static {
        System.out.println("Test 被加载了");
    }
    public static void main(String[] args){
        Test test = new Test();
    }
    private int i;
    Test(){
        System.out.println("我是Test");
        System.out.println("i的值为"+i);
    }
}
class TestFather extends TestGrandFa{
    static {
        System.out.println("TestFather 被加载了");
    }
    private int j;
    TestFather(){
        System.out.println("我是TestFather");
        System.out.println("j的值为"+j);
    }

}
class TestGrandFa{
    static {
        System.out.println("TestGrandFa 被加载了");
    }
    private int k;
    TestGrandFa(){
        System.out.println("我是GrandFa");
        System.out.println("k的值为"+k);
    }
}

The output is

TestGrandFa 被加载了
TestFather 被加载了
Test 被加载了
我是GrandFa
k的值为0
我是TestFather
j的值为0
我是Test
i的值为0

From the result, we can know that
creating an object will start from the direct parent class, and find it layer by layer. Until the parent class is Object,
push these parent classes onto the stack, and initialize the object from the top of the stack. Pop again to continue to the next stack top until the stack is empty.

Published 22 original articles · Likes2 · Visits 881

Guess you like

Origin blog.csdn.net/weixin_41685373/article/details/95521524