Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8311075

Creating a VarHandle for a static field no longer triggers class initialization

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 22
    • core-libs
    • None
    • behavioral
    • low
    • The old eager initialization may have been accidentally used as a feature; changing this behavior may change class loading order in user applications.
    • Java API
    • SE

      Summary

      Creating a VarHandle for a static field no longer triggers class initialization.

      Problem

      Previously, the static field declaring class initialization was done at VarHandle creation time which does not conform the specification that the class should only be initialized when the static field is accessed and the class has not been initialized.

      Solution

      Make MethodHandles::findStaticVarHandle return a lazily-initialized VarHandle that conform to the specifications; it only initializes the declaring class on operations that must require class initialization.

      Specification

      No change.

      In the API specification of MethodHandles::findStaticVarHandle:

      If the returned VarHandle is operated on, the declaring class will be
      initialized, if it has not already been initialized.

      This new implementation conforms to the specification and also is performant than the old eager-initializing implementation.

            liach Chen Liang
            cwimmer Christian Wimmer (Inactive)
            Mandy Chung
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: