Q:

Initialization of static variables in a class which has a private constructor

Suppose the following class
class A{
  public final static int num = 7;
  public final static A instance = new A();
  private A(){}
}

class TestA{
  public static void main(String args[]){
     System.out.print(A.num);
  }
}

In this scenario when is num and instance initialized and when is class A loaded in the JVM?. I had the understanding that the JVM loads a class when it is initialized, correct me if I'm wrong. As the class A has a private constructor it cannot be initialized externally.
Thanks in advance.

A:

The variables are initialized when the class is first loaded by the class loader, which is typically when you first use a class either statically or by instantiating some objects. Imports don't count as using a class. They are ignored by the compiler until you actually do something with the class. 
You can customize the initialization with a static initializer block in the class. This works very much like a constructor, except it gets executed when the class is loaded and before the first constructor call.
class Foo {
  public static final String FOO;

  {
    FOO="helloworld";
  }
}

