Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <ul> <li>Now the question is does serialization loads the class again to have two instance of Elvis?</li> </ul> </blockquote> <p>A new instance of Elvis is created (the impersonator), but the overloaded <code>readResolve</code> method ensures that it is not returned as part of the data structure returned by <code>ObjectInputStream.readObject()</code>.</p> <p>The Elvis impersonator is (or soon becomes) unreachable, and is garbage collected.</p> <blockquote> <ul> <li>If the class is loaded only once then we should be having only one instance of Elvis since static fields are not serialized and are not restored during deserialization and</li> </ul> </blockquote> <p>Ideally yes. In practice no.</p> <blockquote> <ul> <li>From where does the other Elvis instance comes which is made eligible for garbage collection by readResolve (prevented from escaping the deserialization process). Can this be explained?</li> </ul> </blockquote> <p>The deserialization process starts out by creating the 2nd Elvis (the impersonator), but the <code>readResolve</code> method ensures that nothing ever sees it. </p> <p>To understand how and why that is the case, you need to understand the function that a <code>readResolve()</code> method plays in deserialization, as specified <a href="http://download.oracle.com/javase/6/docs/platform/serialization/spec/input.html#5903" rel="nofollow">here</a>. Basically, when the <code>readResolve()</code> method returns <code>INSTANCE</code> it is saying, "wherever you were going to use the impersonator in the graph we are building, use the real Elvis instead".</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload