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

Add java.util.Objects.newIdentity method

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P3 P3
    • tbd
    • core-libs
    • None
    • source
    • minimal
    • Hide
      The new static method poses no risk.
      The class java.util.Objects is final and there is no conflict with existing uses.
      Show
      The new static method poses no risk. The class java.util.Objects is final and there is no conflict with existing uses.
    • Java API
    • SE

      Summary

      Add java.util.Objects.newIdentity to supply a unique object with identity.
      This is a replacement code can be used today for the traditional new Object() idiom, which will be deprecated under Project Valhalla.

      Problem

      Forward looking to JEP 401: Primitive Objects (Preview), the current practice using new Object() to create an object suitable for use as a unique key or synchronization is problematic.

      Refer to [JEP 401: Primitive Classes (Preview)]https://openjdk.java.net/jeps/401

      The naming and context are discussed on the valhalla-spec-experts emails Making Object Abstract.

      Solution

      Creating an API for the specific purpose of creating a unique object with identity enables a smooth transition away from the problematic use case and enables specific recommendations and transitional tools.

      Specification

      The method newIdentity() is added to java.util.Objects.

      /**
       * {@return a new instance of an unspecified class}
       * The object has a unique identity; no other references to it exist.
       * It can be used for synchronization, or where a placeholder Object is needed.
       * The class does not override any of the methods of {@code java.lang.Object}.
       * Use this method to avoid relying on the {@linkplain Object#Object() Object constructor}.
       *
       * @since 17
       */
      public static Object newIdentity() {...}

      In java.lang.Object the javadoc in the constructor includes a link to java.util.Objects.newIdentity()

       /**
        * Constructs a new object.
        * @see Objects#newIdentity()
        */
       public Object() {}

            rriggs Roger Riggs
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: