JAVA static final relationship between static / construction / local code blocks

Keywords: Java Back-end

1 static
1.1 concept
Is a keyword in java
Used to decorate members (member variables and member methods)

1.2 features
1.static can modify member variables and methods
2. Resources modified by static are called static resources
3. Static resources are loaded with the loading of classes. They are loaded first and take precedence over objects
4. Static resources can be called directly through the class name, which is also called class resources
5. Static is shared by all global objects, and there is only one value
6. Static resources can only call static resources
7. this and super keywords are not allowed in the static area

1.3 exercise: introduction to static
Create package: cn.tedu.oop
Create class: TestStatic1.java

package cn.tedu.oop;
/*This class is used for introductory cases of static*/
public class TestStatic1 {
    public static void main(String[] args) {
        Fruit.clean();//We can call static methods directly through the class name, and the IDEA will prompt
        System.out.println(Fruit.kind);//When prompted, the class name directly calls the static attribute

        Fruit f = new Fruit();
        Fruit f2 = new Fruit();
        f.grow();
        f.clean();//No hint, you need to write it yourself

        System.out.println(f.weight);
        System.out.println(f.kind);//No hint

        /*3.Static resources have only one copy in memory and are shared by all global objects
        * So: when we modify the value of a static variable in any way
        * No matter how you view it, the value of the static variable is the value just modified*/
        Fruit.kind = "Apple";
        System.out.println(Fruit.kind);//Apple
        System.out.println(f.kind);//Apple
        System.out.println(f2.kind);//Apple

        f.kind = "Banana";
        System.out.println(Fruit.kind);//Banana
        System.out.println(f.kind);//Banana
        System.out.println(f2.kind);//Banana

        f2.kind = "kiwifruit";
        System.out.println(Fruit.kind);//kiwifruit
        System.out.println(f.kind);//kiwifruit
        System.out.println(f2.kind);//kiwifruit
    }
}
/*0.Resources modified by static are collectively referred to as static resources
* Static resources are loaded into memory with class loading, which takes precedence over objects
* Therefore, static resources can be called directly through the class name, even if no object is created*/
//1. Create a fruit class
class Fruit{
    //2. Define attributes
    /*1.Can member variables be modified with static? sure*/
    static String kind;//varieties
    double weight;//weight

    //3. Define common methods
    /*2.Can I use static modification? sure*/
    public static void clean(){
        System.out.println("Wash the fruit, wash the fruit~");
    }
    public void grow(){
        System.out.println("The fruit looks delicious~");
    }
}

Exercise 1.4: static call relationship

Create package: cn.tedu.oopstatic
Create class: TestStatic2.java

package cn.tedu.oopstatic;
/*This class is used to test the static calling relationship*/
/*Summary:
* 1.Ordinary resources can call both ordinary resources and static resources
* 2.Static resources can only call static resources*/
public class TestStatic2 {
}
//1. Create a teacher class
class Teacher{
    //2. Define common attributes and methods
    String name;
    public void teach(){
        System.out.println("Teaching in progress...");
        /*1.Can ordinary resources call static resources-- sure!!!*/
        System.out.println(age);
        ready();
    }
    //3. Define static attributes and methods
    static int age;
    public static void ready(){
        System.out.println("Preparing lessons...");
        /*2.Can static resources call normal resources-- may not!*/
        //System.out.println(name);
        //teach();
    }
    public static void eat(){
        System.out.println("I'm eating...");
        /*3.Can static resources call static resources-- sure!*/
        System.out.println(age);
        ready();
    }
}

2 static code block, construction code block and local code block

2.1 static code block format

Static resources are loaded with the loading of classes and are only loaded once. They are generally used for project initialization
Features: it is modified by static and located outside the method in the class

2.2 comparison of three code blocks
1. Static code block: it is loaded when the class is loaded and is loaded only once. It is generally used for project initialization
2. Construction code block: it will be called automatically when creating an object. Each time an object is created, it will be called to extract construction commonalities
3. Local code block: a code block in a method that limits the range of local variables
2.3 exercise: relationship of several code blocks
Create package: cn.tedu.block
Create class: TestBlock.java

package cn.tedu.oopstatic;
/*This class is used to learn static code blocks*/
/*Execution sequence:
* Static code block - > construction code block - > construction method [object created successfully] - [local code block]*/
public class TestStaticBlock {
    public static void main(String[] args) {
        //6. Create objects for testing
        Person p = new Person();
        Person p2 = new Person();
        //7. Trigger local code block
        p.play();
    }
}

//1. Create Person class
class Person{
    //8. Create static code blocks
    /*Location: inside class, outside method
    * Execution timing: static code blocks also belong to static resources. They are loaded with class loading, which takes precedence over object loading
    *         And static resources are loaded only once
    * Function: used to load resources that need to be loaded at the first time and only once*/
    static{
        System.out.println("I'm a static code block");
    }
    //2. Create construction code block
    /*Location: inside class, outside method
    Execution timing: triggered every time an object is created, and takes precedence over the execution of the construction method
    Function: used to extract the common functions of all construction methods*/
    {
        System.out.println("I'm building blocks");
    }
    //5. Create construction method
    public Person(){
        System.out.println("I am a nonparametric structure");
    }
    //3. Create common methods
    public void play(){
        System.out.println("I am an ordinary method");
        //4. Create local code blocks
        /*Location: in method
        * Execution timing: only when the method of the local code block is executed
        * Action: used to limit the action range of variables*/
        {
            System.out.println("I am a partial code block~");
        }
    }

}

Conclusion: execution sequence: static code block -- > construction code block -- > construction method -- > local code block

3 final
3.1 concept
1. Is a keyword provided by java
2.final means final
3.final can modify classes, methods, fields (properties)
Original intention: after java inheritance, the subclass can change the function of the parent class. When the function of the parent class does not allow the subclass to change, the final keyword can be used to modify the parent class.
3.2 features
1. The class modified by final cannot be inherited
2. The method modified by final cannot be overridden
3. The field modified by final is a constant, and the value cannot be modified
4. Definition form of constant: final data type constant name = value
3.3 exercise: final introductory case
Create package: cn.tedu.oop
Create class: TestFinal.java

package cn.tedu.oop;
/*This class is used to test the final keyword*/
public class TestFinal {
}

//1. Define parent class
/*1.final It can be used to modify classes. The class modified by final is the final class and cannot be inherited
* The class modified by final can be regarded as a leaf node in the tree structure*/
//3. The test class is modified by final
//final class Father2{
class Father2{
    //4. Common methods for defining parent classes
    /*2.final It can be used to modify a method. The method modified by final is the final implementation of this method and cannot be overridden*/
    //6. The test method is modified by final
    //public final void work(){
    public void work(){
        System.out.println("Work in a factory~");
    }
}
//2. Define subclasses
class Son2 extends Father2{
    final int C = 66;
    //5. Override the method of the parent class
    @Override//This annotation is used to mark that this is an overridden method
    public void work(){
        /*3.What is modified by final is a constant. The value of a constant cannot be modified
        * Note: whether it is a member position or a local position, a constant must be assigned when it is defined
        * Note: constant names must be in all uppercase and used between words_ division*/
        final int B = 100;
        //B = 200;// Error: constant value cannot be modified
        System.out.println("Work in an Internet factory~");
        System.out.println(Integer.MAX_VALUE);
    }
}

 

Posted by echoofavalon on Mon, 08 Nov 2021 17:11:00 -0800