Java Foundation (code block)

Class: code block (or initialization block)

1. Function of code block: used to initialize classes and objects

2. If the generation tree is modified, only static can be used

3. Classification: static code block VS non static code block

4. Static code block

There can be output statements inside
Executes as the class loads, and only once
Function: initialize class information
If multiple static code blocks are defined in a class, they are executed in the order of declaration
The execution of static code blocks takes precedence over the execution of non static code blocks
Static code blocks can only call static attributes and static methods, not non static structures

 

5. Non static code block

There can be output statements inside
Executes as the object is created
Each time an object is created, a non static block of code is executed
Function: you can initialize the properties of the object when creating the object
If multiple non static code blocks are defined in a class, they are executed in the order of declaration
Static attributes, static methods, or non static attributes and non static methods can be called in non static code blocks

Where attributes can be assigned:
① Default initialization
② Explicit initialization / ⑤ assignment in code block
③ Initialization in constructor
④ After you have an object, you can assign values through "object. Attribute" or "object. Method"

Order of execution: ① - ② / ⑤ - ③ - ④

When instantiating a subclass object, it involves the loading sequence of static code blocks, non static code blocks and constructors in the parent class and subclass:
By parent and child, static first.

BlockTest.java

package com.klvchen.java3;

public class BlockTest {
    public static void main(String[] args) {
        String desc = Person.desc;
        
        Person p1 = new Person();
        Person p2 = new Person();
        System.out.println(p1.age);
        
        Person.info();
    }
}

class Person{
    String name;
    int age;
    static String desc = "I am a person";
    
    public Person() {
        
    }
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    //wrong static Code block for
    {
        System.out.println("hello, block - 1");
        age = 1;
        eat();
        desc = "I am a person who loves learning";
        info();
    }
    
    {
        System.out.println("hello, block - 2");
    }
    
    //static Code block for
    static {
        System.out.println("hello, static block-2");
    }
    
    static {
        System.out.println("hello, static block-1");
        desc = "I am a person who loves learning";
        info();
//        eat();
//        name = "Tom";
    }
    
    public void eat() {
        System.out.println("having dinner");
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    
    public static void info() {
        System.out.println("I am a happy person!");
    }
}

StaticBlockTest.java

package com.klvchen.java3;

public class StaticBlockTest {
    public static void main(String[] args) {
        System.out.println("total = " + MyPerson.total);
        System.out.println("total = " + MyPerson.total);
    }

}

class MyPerson{
    public static int total;
    
    static {
        total = 100;
        System.out.println("in static block!");
    }
}

LeafTest.java

package com.klvchen.java3;

class Root{
    static {
        System.out.println("Root Static initialization block for");
    }
    {
        System.out.println("Root Normal initialization block");
    }
    public Root() {
        System.out.println("Root Parameterless constructor for");
    }
}

class Mid extends Root{
    static {
        System.out.println("Mid Static initialization block for");
    }
    {
        System.out.println("Mid Normal initialization block");
    }
    public Mid() {
        System.out.println("Mid Parameterless constructor for");
    }
    public Mid(String msg) {
        //adopt this Call overloaded constructors in the same class
        this();
        System.out.println("Mid The constructor with parameters whose parameter values: " + msg);
    }
}

class Leaf extends Mid{
    static {
        System.out.println("Leaf Static initialization block for");
    }
    {
        System.out.println("Leaf Normal initialization block");
    }
    public Leaf() {
        //adopt super Call the constructor with a string parameter in the parent class
        super("klvchen");
        System.out.println("Leaf Constructor for");
    }
}

public class LeafTest {
    public static void main(String[] args) {
        new Leaf();
        System.out.println();
        new Leaf();
    }

}

Son.java

package com.klvchen.java3;

class Father{
    static {
        System.out.println("11111111111111111111");
    }
    {
        System.out.println("22222222222222222222");
    }
    
    public Father() {
        System.out.println("33333333333333333333");
    }
}

public class Son extends Father {
    static {
        System.out.println("44444444444444444444");
    }
    {
        System.out.println("55555555555555555555");
    }
    public Son() {
        System.out.println("66666666666666666666");
    }
    
    public static void main(String[] args) {
        System.out.println("77777777777777777777");
        System.out.println("**********************");
        new Son();
        System.out.println("**********************");
        new Father();        
    }
}

OrderTest.java

package com.klvchen.java3;

public class OrderTest {
    public static void main(String[] args) {
        Order order = new Order();
        System.out.println(order.orderId);
    }

}


class Order{
    {
        orderId = 4;
    }
    int orderId = 3;
    
}

Tags: Java

Posted on Mon, 29 Nov 2021 07:22:43 -0500 by tfburges