1Z0-830 practice question 7 of 25

Which of the following statements is TRUE about static initializers in Java?

Full set: 116 questions
Answer options
A:

A. They are always executed before instance initializers.

Correct answer
B:

B. They can access instance variables of the same class.

C:

C. They must be declared private.

D:

D. They are used to initialize static fields.

Correct answer
Correct Answer:
A. A. They are always executed before instance initializers.; D. D. They are used to initialize static fields.
Explanation

Both A and D are factually correct statements. Static initializers are blocks of code used to initialize a class's static fields. This initialization occurs when the class is first loaded and initialized by the JVM. Because class initialization must happen before any instance of that class can be created, static initializers are guaranteed to execute before any instance initializers or constructors are run. Therefore, they are used to initialize static fields (D) and they are always executed before instance initializers (A).

Why Incorrect

B. Static initializers execute in a static context and cannot access non-static instance variables directly.
C. Static initializers do not have access modifiers; they are declared with the static keyword followed by a code block.

References

1. Gosling

J.

Joy

B.

Steele

G.

& Bracha

G. (2021). The Java® Language Specification

Java SE 17 Edition. Oracle America

Inc. Section 8.7

"Static Initializers" and Section 12.4.2

"Detailed Initialization Procedure".

2. Oracle. (2022). The Java™ Tutorials: Initializing Fields. Oracle Help Center. The section "Static Initialization Blocks" explains their purpose and execution time.

3. Flanagan

D. (2014). Java in a Nutshell (6th ed.). O'Reilly Media. Chapter 3

"Object-Oriented Programming in Java"

Section "Static Members and Static Initializers".

Scroll to Top