Typically, the JVM loads a class because somebody's trying to make a new instance of the class. As a programmer, you also have the option of telling the JVM to load a class, but you're not likely to need to do that, In nearly all case, you're better off letting the JVM decide when to load the class.
And there are two guarantees about static initialization:
- Static variables in a class are initialized before any object of that class can be created.
- Static variables in a class are initialized before any static method of the class runs.
Static variables are initialized when the class is loaded. If you don't explicitly initialize a static variable(by assigning it a value at the time you declare it), it gets a default value, so int variables are initialized to zero.
Declaring, but not initializing, a static variable means the static variable will get the default value for that variable type, in exactly the same way that instance variables are given default values when declared.