Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What does this program print, and why?

Java
class A {
static {
System.out.println("A init");
}
static final int X = 10;
static int Y = 20;
}
public class Main {
public static void main(String[] a) {
System.out.println(A.X);
System.out.println(A.Y);
}
}

The program prints 10, then A init, then 20. A.X is a compile-time constant, a static final int initialised with a constant expression, so javac copies the value 10 into Main and reading it never touches class A. Reading A.Y is a real access to a non-constant static field, so the JVM initialises A first, which runs the static block and prints A init before 20 is printed.

Why this question is asked

Questions about static initialisation order look like trivia, but they explain real bugs: configuration values that do not change after a library upgrade, static blocks that run at surprising moments, and start-up order problems. Interviewers ask this exact program because the answer depends on one rule most developers have never been told explicitly. In this answer, we will recap what class initialisation is, run the program line by line, see why the constant is inlined, list what else triggers initialisation, and look at the textual order rule and the follow-up with Integer.

1A Quick Recap of Class Initialisation

A class goes through several stages before we can use it. Loading finds its bytes. Linking verifies them and prepares the static fields with default values such as 0 and null. Initialisation then runs the class's static initialiser code: every static field initialiser and every static block, combined by the compiler into one hidden method called <clinit>. Initialisation happens at most once per class, and lazily: the JVM runs it only when the class is first actively used.

The interesting word is actively. The Java Language Specification lists exactly which actions count, and reading a static field counts only if the field is not a constant variable. That exception is the whole puzzle.

2Running the Program Line by Line

Here is the program again, with what each line prints.

Java
class A {
static { System.out.println("A init"); }
static final int X = 10; // constant variable: inlined by javac
static int Y = 20; // ordinary static field
}
public class Main {
public static void main(String[] a) {
System.out.println(A.X); // 10 (A is not initialised)
System.out.println(A.Y); // A init, then 20
}
}
// Output:
// 10
// A init
// 20

The first println reads A.X, but A is never initialised, so nothing else is printed. The second println reads A.Y. Before the read can happen, the JVM sees that A has not been initialised yet, initialises it, which runs the static block, and only then reads the field. So A init appears between 10 and 20.

3Why Reading A.X Does Not Initialise A

A field that is final, of a primitive type or String, and initialised with a constant expression (a literal, or arithmetic on other constants) is called a constant variable. For such fields, javac does not generate a field read at all. It copies the value into the class that uses it. We can see this with javap, the disassembler that ships with the JDK.

Shell
$ javap -c Main
getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
bipush 10 // the constant 10 is copied into Main: no reference to A
invokevirtual #13 // Method java/io/PrintStream.println:(I)V
getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
getstatic #19 // Field A.Y:I <- a real read of A's field: A is initialised here
invokevirtual #13 // Method java/io/PrintStream.println:(I)V
Step throughWhat happens on the second println
  1. getstatic A.Ya real field access
  2. Is A initialised?
    YesRead YNoInitialise A first
  3. Initialise superclass Objectalready done
  4. Run A's <clinit> in textual orderprints A init, sets Y = 20
  5. Read A.Y and print it20

5 steps in order. Play them, or step through them one at a time.

Notice that the bytecode for the first line contains bipush 10, a plain number, and no reference to A at all. The JVM cannot initialise a class the code never mentions. The second line is a getstatic instruction on A.Y, and getstatic is one of the instructions that triggers initialisation.

But there is a catch that bites in real projects. Because the value is copied into Main at compile time, if we later change X to 11 in A and recompile only A, Main still prints 10 until it is recompiled too. This is why public constants in a library should be values that never change, and why changing such a constant is effectively a breaking change.

4What Else Triggers Initialisation

Reading a non-constant static field is one trigger among several. A class is initialised the first time code creates an instance with new, calls one of its static methods, assigns to one of its static fields, or reads a non-constant static field. Reflection also counts: Class.forName("A") initialises the class, because the one-argument form passes initialize as true, while Class.forName("A", false, loader) only loads it. Initialising a subclass first initialises its superclass, and the class containing main is initialised when the program starts.

Java
Class<?> c1 = Class.forName("A", false, Main.class.getClassLoader()); // nothing printed
Class<?> c2 = A.class; // nothing printed
A[] arr = new A[3]; // nothing printed
Class<?> c3 = Class.forName("A"); // A init

Notice that the class literal A.class and creating an array of A do not initialise A; they only need the class loaded. This is why frameworks that scan classes with reflection can inspect them without running their static blocks.

5Textual Order Inside the Static Initialiser

When initialisation does run, the superclass is initialised first, then the class's static field initialisers and static blocks run in the order they appear in the source. Order matters when one static field depends on another.

Java
class B {
static int first = second + 1; // second is still 0 here
static int second = 5;
}
System.out.println(B.first); // 1, not 6
System.out.println(B.second); // 5

When first is computed, second has only its default value 0, because its initialiser has not run yet. So first becomes 1. Moving the declaration of second above first changes the result to 6. In our original program, the static block is written before Y, so A init is printed before Y is set to 20, although nothing observes the difference there.

6Follow-up questions

What changes if X is declared static final Integer X = 10?

Integer is not a primitive or String, so X is no longer a constant variable even though it is final. javac must emit a real getstatic A.X, and that triggers initialisation. The program then prints A init, then 10, then 20. The same happens with static final int X = compute(); because a method call is not a constant expression.

Is a static final String a constant too?

Yes, if it is initialised with a constant expression, such as a literal or a concatenation of literals and other constants. static final String NAME = "app" is inlined and reading it does not initialise the class. static final String NAME = System.getenv("APP") is not a constant and reading it does initialise the class.

How can we see when classes are initialised?

A print in the static block is the simplest way. The JVM can also log class loading with -Xlog:class+load and initialisation with -Xlog:class+init (check your version for the exact tags), which is useful when a start-up order problem involves classes we cannot edit.

7Conclusion

In this answer, we saw that the program prints 10, A init and 20. A.X is a constant variable, a final primitive initialised with a constant expression, so javac copies 10 into Main and A is never touched, while reading A.Y is a real getstatic that initialises A and runs its static block first. Other triggers are new, static method calls, static field writes, Class.forName with initialisation, and subclass initialisation, and when a class is initialised its superclass goes first and its static initialisers run in textual order.

What to say in the interviewAbout 90 seconds aloud

Open with

It prints 10, then A init, then 20. The reason is that X is a compile-time constant: it is static final, of a primitive type, and initialised with a constant expression.

Then, if they want more

For constant variables javac does not generate a field access. It copies the value 10 into Main's bytecode, so the first println never references class A and A is not initialised. Y is an ordinary static field, so reading it compiles to a getstatic on A.Y, and the first getstatic on an uninitialised class makes the JVM initialise it.

Initialisation runs A's static initialiser, the combined static blocks and static field initialisers in textual order, so A init is printed, Y is set to 20, and then 20 is printed.

Other things that trigger initialisation are creating an instance with new, calling a static method, writing a static field, Class.forName with initialize true, which is the default one-argument form, and initialising a subclass, because the superclass is always initialised first. Things like A.class or creating an array of A do not initialise it.

A practical consequence of inlining is that if you change a public constant in a library and do not recompile the callers, they keep the old value. And if X were declared as final Integer, it would not be a constant, so the output would be A init, 10, 20.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does this program print, and why?": https://scrivio-interview-library.netlify.app/java/class-loading/0dcd457656/