美文网首页Java
Java update static member value

Java update static member value

作者: JaedenKil | 来源:发表于2019-06-30 12:55 被阅读0次
  • Demo 1:
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'.
  • Demo 2:
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'.
  • Demo 3:
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'.

相关文章

网友评论

    本文标题:Java update static member value

    本文链接:https://www.haomeiwen.com/subject/nntbcctx.html