Java / JVM Architecture (Java21) Interview questions
Explain the lifecycle of a class in JVM (loading, linking, initialization)?
A class moves through three ordered stages before any of its code can run, all triggered lazily on first active use.
flowchart TD A[Loading] --> B[Linking] B --> B1[Verification] B --> B2[Preparation] B --> B3[Resolution] B1 --> C[Initialization] B2 --> C B3 --> C C --> D[Use: instances created, methods called] D --> E[Unloading, when its ClassLoader becomes unreachable]
Loading reads the bytecode and creates the Class object; linking covers verification (bytecode safety checks), preparation (allocating static fields with default values), and resolution (turning symbolic references into direct ones); initialization then runs static initializer blocks and assigns real static field values in source order.
Only after initialization completes can the class be instantiated or have its static methods invoked normally.
More Related questions...