Java

Java update static member value

2019-06-30  本文已影响0人  JaedenKil
import java.time.LocalDateTime;

class ClassA {
    // initial value
    private int second = -1;

    ClassA() {
        if (second == -1) {
            LocalDateTime now = LocalDateTime.now();
            second = now.getSecond();
        }
    }

    void show() {
        System.out.println("Current second is '" + second + "'.");
    }
}

----------------
public class ClassB {

    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i <= 10; i++) {
            ClassA a = new ClassA();
            a.show();
            Thread.sleep(500);
        }
    }

}

Current second is '37'.
Current second is '38'.
Current second is '38'.
Current second is '39'.
Current second is '39'.
Current second is '40'.
Current second is '40'.
Current second is '41'.
Current second is '41'.
Current second is '42'.
import java.time.LocalDateTime;

class ClassA {

    private static int second = -1;

    ClassA() {
        // assign value
        if (second == -1) {
            LocalDateTime now = LocalDateTime.now();
            second = now.getSecond();
        }
    }

    void show() {
        System.out.println("Current second is '" + second + "'.");
    }
}
--------
public class ClassB {

    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i <= 10; i++) {
            ClassA a = new ClassA();
            a.show();
            Thread.sleep(500);
        }
    }

}
Current second is '28'.
Current second is '28'.
Current second is '28'.
Current second is '28'.
Current second is '28'.
Current second is '28'.
Current second is '28'.
Current second is '28'.
Current second is '28'.
Current second is '28'.
import java.time.LocalDateTime;

class ClassA {

    private static int second = -1;

    ClassA() {

    }

    void show01() {
        System.out.println("Current second is '" + second + "'.");
    }

    void check() {
        if (second == -1) {
            LocalDateTime now = LocalDateTime.now();
            second = now.getSecond();
        }
    }

    void show02() {
        System.out.println("Current second is '" + second + "'.");
    }

}
--------
public class ClassB {

    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i <= 10; i++) {
            ClassA a = new ClassA();
            a.show01();
            a.check();
            a.show02();
            Thread.sleep(500);
        }
    }

}

Current second is '-1'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
Current second is '39'.
上一篇下一篇

猜你喜欢

热点阅读