News for hackers. Stuff that matters.
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by ashishb on Thursday July 02, 2026 @04:13PM (#48766742)
Weirdly enough, I encountered this once while writing code. Here's a simplified example of the same - https://ashishb.net/programming/java-musings-initializing-a-...
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by cogman10 on Thursday July 02, 2026 @03:56PM (#48766537)
This is a great change that will undoubtedly cause a lot of headaches.

There's a number of libraries (particularly around serialization/marshaling) which will end up mutating `final` fields. In fact, this is a trick I've pulled once or twice in my own code for "reasons" (generally needing to modify behavior of a library because it was deficient).

I suspect this will be one of those things that ends up requiring java devs everywhere to bump up the versions of the libraries they use.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by kasperni on Thursday July 02, 2026 @04:01PM (#48766597)
Strict Field Initialization is opt-in. A flag needs to be set in the classfile in order to enable it. So should not effect any existing code.
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by cogman10 on Thursday July 02, 2026 @04:09PM (#48766707)
It won't bite initially, it will bite when you go to update your version of javac in the future and this becomes the default. Or when you update a library that just so happened to be compiled with a newer version of javac.

This particularly matters when you have something likes this

    class Local {
       private final ThirdPartyObject tpo;
    }
or even something like this

    class Local {
      private final LocalDate ld;
    }
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by vips7L on Thursday July 02, 2026 @04:31PM (#48766975)
Extremely easy to fix. Turn it into a record. I’m also pretty sure that cracking final fields is already disabled by default.
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by cogman10 on Thursday July 02, 2026 @04:48PM (#48767169)
> Extremely easy to fix.

Nope, if the deserializer is initializing that field by directly setting values both by the field and by the internals of the field, it'll be a problem. The fix is to update the deserializer to a newer version. Apache Fury recently fixed this very issue, but it still relies on internal JDK APIs in order to do it's work.

> I’m also pretty sure that cracking final fields is already disabled by default.

Nope. There's sun.misc.Unsafe apis that allow for cracking and modifying those final fields. There are new jdk.internal APIs for doing the same that you'd have to move over to in order to accomplish the same. This JEP is about making final (mostly) meaning final. At very least, it will enforce that final once observed is final with the internal APIs allowing a final field to be set once, just outside the constructor.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by vips7L on Thursday July 02, 2026 @08:04PM (#48769017)
> sun.misc.Unsafe

Yeah idk man sounds like it sucks to suck in this case. Don’t use unsafe.

From your example: record Local(LocalDate ld) {}

Will work perfectly fine with every deserializer I’ve ever seen.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by pjmlp on Friday July 03, 2026 @02:30AM (#48771578)
sun.misc.Unsafe will eventually be no longer, plenty of methods have already been removed since safer alternatives were introduced.

Java isn't alone in this, as usual due to their relationship, .NET is also clamping down some of this stuff, including changing the memory model around unsafe code blocks.

There are other ecosystems for monkey patching.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by debugnik on Thursday July 02, 2026 @04:48PM (#48767168)
That's a separate series of JEPs known as "final means final", also starting to land nowadays.

https://openjdk.org/jeps/500

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by cogman10 on Thursday July 02, 2026 @04:51PM (#48767213)
I believe these 2 are effectively linked.

There's a jdk.internal API which will work as an escape hatch, but that does come with the need for non-compliant libraries to switch over to it. That safety hatch also only allows the setting of final fields once before observation (which is generally fine). So if your code is doing something more esoteric that sets a final field multiple times you will be SOL.

In any case, if you are using the sun.misc.Unsafe methods for setting final and private fields you'll need to update.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by well_ackshually on Thursday July 02, 2026 @06:47PM (#48768372)
>particularly around serialization/marshaling

The solution being to drop those dogshit reflection based libraries as everyone should have done 10 years ago when codegen became more common.

Looking at you, Gson.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by vips7L on Thursday July 02, 2026 @08:12PM (#48769070)
I swapped to Avaje Jsonb a while ago. Can’t recommend it and the rest of the Avaje libraries enough, especially Ebean instead of Hibernate.
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by rvcdbn on Thursday July 02, 2026 @04:13PM (#48766740)
oracle planning a new jvm language? have we ever seen a feature like this that is explicitly not usable from Java?
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by Skinney on Thursday July 02, 2026 @04:31PM (#48766973)
This is required in order to implement value classes in Java (project valhala).
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by hueho on Thursday July 02, 2026 @04:37PM (#48767055)
It's a VM feature - value classes will use it when they land eventually. https://openjdk.org/projects/valhalla/
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by cogman10 on Thursday July 02, 2026 @05:37PM (#48767706)
> have we ever seen a feature like this that is explicitly not usable from Java?

Loads. Invoke dynamic and Nest-Based Access Control come to mind.

This sort of thing is also about tightening the VM specification so things like Valhala are possible. Value classes really can't function reasonably without strict field initialization. That's because these value classes can have multiple copies while an application is running. If there's a way to go in and tinker with the fields before, after, or during initialization it could lead to very hard to fix and debug issues. 1 object in 2 parts of the code with different field values.

And the reason for this tightening is because, from java, there are routes to violate this strict field initialization.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by vips7L on Thursday July 02, 2026 @08:13PM (#48769077)
All the time. Oracle and the OpenJdk team do VM changes way more than language changes.
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by someonebaggy on Friday July 03, 2026 @05:37AM (#48772961)
invokedynamic comes to mind.

It was later used to implement lambda expressions but originally had no use in the Java language and isn't tailored to lambda expressions.

Re:JEP 539: Strict Field Initialization in the JVM moved to ... by joe_mwangi on Thursday July 02, 2026 @05:01PM (#48767325)
Also, this will be used for future null-restricted types.
Re:JEP 539: Strict Field Initialization in the JVM moved to ... by exabrial on Thursday July 02, 2026 @08:05PM (#48769025)
This a much needed change. Glad to see it! Might be some headaches in the short term, but thats ok.