0

If I do not initialize count variable in my code and let it initialize by default value, my code displays correctly count = 1. But if I initialize to 0 in my code -> private static int count = 0, it prints 0. Can any one please explain what is the reason?

class Singleton3 {
    private final static Singleton3 obj = new Singleton3();
    private static int count;

    private Singleton3() {
        count++;
    }

    public static Singleton3 getInstance() {
        return obj;
    }

    public static int getCount() {
        return count;
    }
}

public class SingletonImplEagerInitialization {
    public static void main(String[] args) {
        Singleton3.getInstance();
        Singleton3.getInstance();
        Singleton3.getInstance();
        Singleton3.getInstance();
        Singleton3.getInstance();
        System.out.println("count: " + Singleton3.getCount());

    }
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
Jim Mehta
  • 56
  • 2
  • 8
  • 1
    Thank you. I am sorry I did not get my answer from the suggested duplicate by shmosel. My question is what is so different in which by default JVM initializes a static variable verses it is initialized in the code? – Jim Mehta Jul 23 '18 at 22:57
  • 2
    Field initializers run in order. If you initialize `count` to 0 after incrementing it, it effectively resets it. If you don't... it doesn't. – shmosel Jul 23 '18 at 23:05
  • Thank you. I imagined that it must be running after the constructor is called, but I did not know JVM will not initialize it to 0 after the constructor. If it was not initialized before how it adds 1 to the count? I placed display statements before and after the count++ in the constructor. It adds 1 but then again it is reset to 0? – Jim Mehta Jul 23 '18 at 23:19
  • If it is able to add 1 that means the static variable is already initialized to zero and it does not contain garbage. So after that if the initialization is done in the code is executed, it is meaningless. – Jim Mehta Jul 23 '18 at 23:43
  • Correct, the field is initialized to 0 by default, even before it's explicitly initialized. – shmosel Jul 23 '18 at 23:57
  • Thank you. That resolves my question. Never initialize static variables in your code. Conceptually it is wrong, as it will always give a wrong result. – Jim Mehta Jul 24 '18 at 00:07
  • I don't know how you came to that conclusion. There's nothing wrong with initializing static variables. The problem here is that you're manipulating a static field before the class is initialized. – shmosel Jul 24 '18 at 00:09
  • Please explain if I want to initialize -> private static int count = 0; in my java code, how could I do it? – Jim Mehta Jul 24 '18 at 00:17
  • Just switch the order so it runs before the constructor. – shmosel Jul 24 '18 at 00:19
  • Thank you. In the code it is coded before the constructor. But, isn't it first initializer blocks, then the constructor runs before the static initializers and static variables, always? So how can I switch the order? Please make the changes in the code, so that I can understand. – Jim Mehta Jul 24 '18 at 00:45
  • No, the static initializers run in order. If you call the constructor after initializing `count`, it'll result in 1, as expected. – shmosel Jul 24 '18 at 00:46

0 Answers0