It is said that static blocks in java run only once when that class is loaded. But what does it actually mean? At which point is a class loaded by JVM (Java Virtual Machine)?
Is it when the main method in that class is called? And is it that all the super-classes of the same class are also loaded when the main method starts execution?
Consider that A extends B and B extends C. All have static blocks. If A has the main method, then what will be the sequence of execution of static blocks?
This is described in the Execution section of the JLS. Namely:
So in your example, the static block of the “topmost” class (
C) runs first, then that ofB, then the most-derived one.See that documentation for a detailed description of all the steps that go into loading a class.
(Classes get loaded when they are first actively used.)