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() {}
- csr of
-
JDK-8269096 Add java.util.Objects.newIdentity method
-
- Closed
-
- links to