0

I have a globle flag to enable logging / disable logging and a class level flag as well.

This is the code:

public class Utils {
    public static volatile boolean GLOBLE_LOG_FLAG = true;
}

public class TestClass {
    private static volatile Boolean LOCAL_LOG_FLAG = Utils.GLOBLE_LOG_FLAG;

    public void test() {
        System.out.println("LOCAL_LOG_FLAG: = " + LOCAL_LOG_FLAG);

        Utils.GLOBLE_LOG_FLAG = false;

        System.out.println("now LOCAL_LOG_FLAG: = " + LOCAL_LOG_FLAG);
    }
}

public static void main(String[] args) {
        TestClass class1 = new TestClass();
        class1.test();
}

Output:

LOCAL_LOG_FLAG: = true
now LOCAL_LOG_FLAG: = true

Why LOCAL_LOG_FLAG does not change when i set Utils.GLOBLE_LOG_FLAG = false;

If java is using object reference it should change LOCAL_LOG_FLAG to false when I assign Utils.GLOBLE_LOG_FLAG = false; isn't it ? What's my mistake here?

Thanks

fifa chapman
  • 231
  • 1
  • 2
  • 8

0 Answers0