How «final» is final?
summary
Shows that Java's "final" differs between the language and the JVM: the compiler enforces single assignment while bytecode/JVM allows multiple assigns inside constructors and static initializers, and JIT can replace field reads with constants (demo: warmup loop of 20,000 iterations yields stale 42 vs updated 99). Demonstrates concrete hacks that mutate finals: JNI, reflection (setAccessible + clearing the FINAL modifier), Unsafe/MethodHandles, notes Java 8 ASM availability and a JDK9 module/startup bug, and shows these tricks can crash or silently break optimizations.
AI-written summary · about
chapters (10)
- 0:00Introduction to final in Java
- 1:51Final variable initialization rules
- 6:48Final field escape during construction
- 10:05Changing final values with JNI
- 11:52Changing final values with reflection
- 19:38Final fields as compile-time constants
- 23:54JIT compiler optimizations on final fields
- 33:59Solutions to prevent final field issues
- 48:19Performance impact of final
- 58:09Q&A
