33.3k views
1 vote
When are statements in a class-level static initialization block executed?

a) Before the code in the class constructor is executed.
b) Once when the class is loaded.
c) Whenever objects of the class are used to access methods.
d) After the code in the class constructor is executed.

1 Answer

3 votes

Final answer:

Static initialization blocks in a class are executed once when the class is loaded into the JVM, which occurs before any instances of the class are created or any static methods or fields are accessed.

Step-by-step explanation:

Statements in a class-level static initialization block are executed once when the class is loaded into the JVM. This happens before any instances are created and before any static methods are accessed. It's a mechanism to initialize static fields and to perform any setup required for the class as a whole.

The correct choice that answers when the static initialization blocks are executed is: b) Once when the class is loaded. If a class has a static block, it runs when the class is first loaded, which is before any objects are created from that class and before any static methods or static variables of the class are accessed.

User Mpac
by
8.2k points