The internal pool for Java constant constants is what the Java compiler knows, so you cannot accurately simulate the exact behavior.
The pool itself is nothing but a hash map. If your object has a suitable identifier, you can, of course, merge the pool for your own objects: just create a static method that takes the key, looks at it in the static hash map and builds a new object only if it has not been merged yet. Note, however, that in orde, for this simple working scheme, it is important that your object is immutable.
source
share