Q: Why are $assertionsDisabled nested classes loaded eagerly by interface static initializers?

David Lloyd david.lloyd at redhat.com
Wed Jan 28 21:43:10 UTC 2026


On the same token, maybe it's worth considering going farther, and making
assertion status determination use a dynamic constant instead of a
synthetic field?

On Wed, Jan 28, 2026 at 3:36 PM Eirik Bjørsnøs <eirbjo at gmail.com> wrote:

>
> Hi,
>
> Since interfaces cannot have private fields, javac needs to generate a
> synthetic inner class to hold the $assertionsDisabled field.
>
> A static initializer is also added to the interface, eagerly initializing
> the nested class, see the java.util.zip.Checksum for an example:
>
>  static {};
>     Code:
>          0: getstatic     #16                 // Field
> java/util/zip/Checksum$1.$assertionsDisabled:Z
>          3: ifeq          6
>          6: return
>
> What is the purpose of this eager initialization in Lower::assertFlagTest?
> I would presume a lazy initialization of the nested class on the first run
> of an assert would be sufficient?
>
> In fact when I remove this eager initialization, assertions seem to load
> the nested class just fine with -esa enabled, but avoid loading the nested
> class with assertions disabled.
>
> Asking because this would save class loading common scenarios where the
> default method is always overridden and thus the default method with the
> assert is never executed.
>
> Can we load these nested synthetic classes lazily?
>
> Cheers,
> Eirik.
>


-- 
- DML • he/him
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20260128/0239d7d0/attachment.htm>


More information about the compiler-dev mailing list