Calling instance method from value class constructor is a little confusing

Lois Foltan lois.foltan at oracle.com
Mon Oct 27 12:42:23 UTC 2025


Hi Ethan,

Thanks for your feedback!  I’ve created JBS issue https://bugs.openjdk.org/browse/JDK-8370687 to track improving this error message.

Lois



Confidential – Oracle Internal
From: valhalla-dev <valhalla-dev-retn at openjdk.org> on behalf of Ethan McCue <ethan at mccue.dev>
Date: Saturday, October 25, 2025 at 11:13 PM
To: valhalla-dev at openjdk.org <valhalla-dev at openjdk.org>
Subject: Calling instance method from value class constructor is a little confusing
Just a note from playing with the EA build. Minimal example:

import java.util.ArrayList;

value class HittableList2 {
    private final ArrayList<Object> objects
            = new ArrayList<>();

    HittableList2() {}

    HittableList2(Object object) {
        add(object);
    }

    void add(Object object) {
        objects.add(object);
    }
}

This gives the error

/Users/emccue/Development/raytracer/src/HittableList2.java:10: error: cannot reference add(Object) before constructor has been called
        add(object);
        ^

I know this means the superclass's constructor (and that I need to put an explicit super() at the top of the method), but reading that straight "cannot reference ... before constructor has been called - I'm in the constructor!"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20251027/df9d3c12/attachment.htm>


More information about the valhalla-dev mailing list