Class Loading & Initialization
How a type gets from raw bytes to a usable runtime class: loading, verification, preparation, resolution, initialization, and the loader hierarchy that gives each type its identity. Interviewers reach for it when they want depth, since NoClassDefFoundError and "X cannot be cast to X" only make sense once you know this.
on this pageshowhide
explore
- Loading Phase5 questions
- Linking: Verify, Prepare, Resolve5 questions
- Initialization & Static Setup2 questions
- ClassLoader Hierarchy4 questions
- Parent Delegation Model5 questions
- Custom ClassLoaders3 questions
- Class Identity & Uniqueness5 questions
- Load-Time Failures & LinkageError4 questions
questions
page 2 of 2A framework generates a proxy class as a byte[] at runtime. What are the ways to turn those bytes into a usable runtime type on a modern JVM, and how do the options differ in visibility, access and lifetime?
basics
~20 sThree routes: define through a custom ClassLoader's protected defineClass; call MethodHandles.Lookup#defineClass (Java 9+) to define into the lookup class's own loader and package with its access; or Lookup#defineHiddenClass (Java 15+) for a class with no name in the loader's table, private access to its host, and lifetime tied to it.
Some JVM runtime types never come from a classfile at all — array types such as `String[]` are one example. How does the JVM create such types, and how do runtime-generated types like lambda implementation classes come into existence?
basics
~20 sArray classes and primitive types have no binary representation: the JVM synthesises them directly, and an array class's defining loader is its component type's loader (bootstrap for primitive arrays). Lambda implementation classes do come from bytes, but bytes generated in memory at first execution and defined as hidden classes.
A JVM service generates a large number of classes at run time and its start-up time is dominated by class loading. How does the bytecode verification phase factor into that cost, and what levers would you consider?
basics
~20 sVerification is per-class work proportional to bytecode size and branch structure, and it is a significant part of class-load cost for application classes — bootstrap classes are trusted and skipped by default. Levers: generate fewer and smaller classes, reuse them, and use class-data archives that store already-linked classes, rather than disabling verification, which modern JDKs ignore.
showing 31–33 of 33