current position:Home>[java learning] some understanding of static

[java learning] some understanding of static

2022-01-26 22:53:31 I'm moving the code again

When static When modifying variables or methods , It is called class variable or class method , When the class is loaded , Load along with class .

class StaticTest {
    

    static String MY_STATIC_STRING = " result : " + getMyBoolean();

    static boolean getMyBoolean() {
    
        boolean isRandom = new Random().nextBoolean();
        System.out.println("isRandom: " + isRandom);
        return isRandom;
    }
}

Define a static variable in a class MY_STATIC_STRING
A static method getMyBoolean()
among , Some of the values of static variables are obtained from static methods

Define another test class

public class StaTest {
    

    public static void main(String[] args) {
    
        for (int i = 0; i < 10; i++) {
    
            System.out.println("getMyBoolean: " + StaticTest.getMyBoolean());
            System.out.println(StaticTest.MY_STATIC_STRING);
            System.out.println("--------------------");
        }
    }
}

Through a cycle , Make multiple calls to... In the class getMyString() Static methods and MY_STATIC_STRING Static variables

The operation results are as follows

isRandom: true
isRandom: false
getMyBoolean: false
 result : true
--------------------
isRandom: false
getMyBoolean: false
 result : true
--------------------
isRandom: true
getMyBoolean: true
 result : true
--------------------
isRandom: false
getMyBoolean: false
 result : true
--------------------
isRandom: false
getMyBoolean: false
 result : true
--------------------
isRandom: true
getMyBoolean: true
 result : true
--------------------
isRandom: true
getMyBoolean: true
 result : true
--------------------
isRandom: true
getMyBoolean: true
 result : true
--------------------
isRandom: true
getMyBoolean: true
 result : true
--------------------
isRandom: true
getMyBoolean: true
 result : true
--------------------

analysis

As can be seen from the results , On the first run , class StaticTest Loaded once , Then it doesn't load again , So it seems that the first run result has two times isRandom A hint of
Because during class loading , Load static methods getMyBoolean() Get a return value of true, Then assign it to the static variable MY_STATIC_STRING, After the class is loaded , Static variables are loaded into the method area

Things stored in the method area
1. Store code snippets
When a class is compiled, a bytecode file is generated and loaded into... Through the class loader jvm The memory of the , At this time, the bytecode file of the class will be generated in the method area .
2. Store static member variables ( It's also called a class variable ).

After the static variable is loaded , here MY_STATIC_STRING=" result : " + getMyBoolean() Saved in the method area , At this time getMyBoolean() What is returned is true, So in the method area MY_STATIC_STRING= result : true

however getMyBoolean() The results change every time you run , And static variables MY_STATIC_STRING This static method is called again , Does that mean every time you call MY_STATIC_STRING Will get the latest getMyBoolean() What about the return value ?

The answer can be seen from the running results , stay MY_STATIC_STRING After being loaded , Its value will not change , And every time you call MY_STATIC_STRING when , It doesn't call getMyBoolean() Method , So even getMyBoolean() The result returned has changed ,MY_STATIC_STRING You won't get the latest changes . No matter how many times you call ,MY_STATIC_STRING The value of is always the value obtained when the class is loaded .

What should I write if I need to get the latest changed value ?

   // Improvements are as follows 
    public static String getMyStaticString() {
    
        return " result : " + getMyBoolean();
    }

The complete code after the change

class StaticTest {
    

    static String MY_STATIC_STRING = " result : " + getMyBoolean();

    static boolean getMyBoolean() {
    
        boolean isRandom = new Random().nextBoolean();
        System.out.println("isRandom: " + isRandom);
        return isRandom;
    }

    // Improvements are as follows 
    public static String getMyStaticString() {
    
        return " result : " + getMyBoolean();
    }

}

public class StaTest {
    

    public static void main(String[] args) {
    
        System.out.println(StaticTest.MY_STATIC_STRING);
        System.out.println("======================");
        for (int i = 0; i < 10; i++) {
    
            System.out.println("getMyBoolean: " + StaticTest.getMyBoolean());
            System.out.println(StaticTest.getMyStaticString());
            System.out.println("--------------------");
        }
    }
}

Running results

isRandom: true
 result : true
======================
isRandom: true
getMyBoolean: true
isRandom: false
 result : false
--------------------
isRandom: false
getMyBoolean: false
isRandom: true
 result : true
--------------------
isRandom: false
getMyBoolean: false
isRandom: true
 result : true
--------------------
isRandom: false
getMyBoolean: false
isRandom: false
 result : false
--------------------
isRandom: true
getMyBoolean: true
isRandom: true
 result : true
--------------------
isRandom: false
getMyBoolean: false
isRandom: false
 result : false
--------------------
isRandom: false
getMyBoolean: false
isRandom: true
 result : true
--------------------
isRandom: true
getMyBoolean: true
isRandom: false
 result : false
--------------------
isRandom: false
getMyBoolean: false
isRandom: true
 result : true
--------------------
isRandom: false
getMyBoolean: false
isRandom: true
 result : true
--------------------

copyright notice
author[I'm moving the code again],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262253274327.html

Random recommended