Is it possible to implement an instance of a Java constructor in the same instance?

I implemented the copy constructor in Java 8 and used it as follows:

User user = // some object
LOG.debug("{}", user);
this.userAccount = new User(user);
LOG.debug("{}", this.userAccount);

Conclusion:

User@2799061
User@2799061

This is the same object! How can it be? Is this some kind of optimization that I don't know about?

In addition, my User class is a JPA managed entity. Could this somehow intervene?

+4
source share
1 answer

This is the same object!

, . , LOG.debug. LOG.debug toString, , toString. - toString, , -, Object#toString @, - . . Object#toString Javadoc, :

toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character @' - . , , :

getClass().getName() + '@' + Integer.toHexString(hashCode())

:

Java ?

, Java.

+8

Source: https://habr.com/ru/post/1542040/


All Articles