Final Means Final: Why 2 + 2 Must Soon Stop Equaling 5
2 + 2 equals 5. And a string literal changes its content – not in one place, but everywhere in the program. Both are possible in Java, using deep reflection: setAccessible(true) and overwriting final fields.
What looks like a party trick is a serious risk to runtime integrity. Core invariants can be subverted, security assumptions broken, seemingly immutable objects manipulated – and that is exactly what makes deep reflection an attractive tool for attackers.
But even without malicious intent, it gets expensive: constant folding, one of the most fundamental JVM optimizations, only works if a value truly stays constant.
We won’t just look at this danger in theory, but in very concrete terms. Through live coding, I’ll show how deep reflection can be used to change internal state – and how an attacker can use it to subvert security assumptions.
This is exactly where JEP 500, “Prepare to Make Final Mean Final,” comes in. The JVM warns when code overwrites a final field via deep reflection; in a future version, it will throw an exception. Deep reflection thus loses its most destructive capability – at least where integrity matters more than maximum flexibility. You’ll see live how the same attacks are exposed on a modern Java version – and how to prevent them.
Finally, we’ll connect the dots to “Integrity by Default” – the next step of a plan Java has been pursuing since the module system arrived in version 9.



