Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>transient</code> keyword in Java is used to indicate that a field should not be serialized.</p> <p>From the <a href="http://docs.oracle.com/javase/specs/jls/se7/html/index.html" rel="noreferrer">Java Language Specification, Java SE 7 Edition</a>, <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.3" rel="noreferrer">Section 8.3.1.3. <code>transient</code> Fields</a>:</p> <blockquote> <p>Variables may be marked <code>transient</code> to indicate that they are not part of the persistent state of an object.</p> </blockquote> <p>For example, you may have fields that are derived from other fields, and should only be done so programmatically, rather than having the state be persisted via serialization. </p> <p>Here's a <code>GalleryImage</code> class which contains an image and a thumbnail derived from the image:</p> <pre><code>class GalleryImage implements Serializable { private Image image; private transient Image thumbnailImage; private void generateThumbnail() { // Generate thumbnail. } private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException { inputStream.defaultReadObject(); generateThumbnail(); } } </code></pre> <p>In this example, the <code>thumbnailImage</code> is a thumbnail image that is generated by invoking the <code>generateThumbnail</code> method.</p> <p>The <code>thumbnailImage</code> field is marked as <code>transient</code>, so only the original <code>image</code> is serialized rather than persisting both the original image and the thumbnail image. This means that less storage would be needed to save the serialized object. (Of course, this may or may not be desirable depending on the requirements of the system -- this is just an example.)</p> <p>At the time of deserialization, the <a href="http://java.sun.com/javase/6/docs/api/java/io/ObjectInputStream.html#readObject()" rel="noreferrer"><code>readObject</code></a> method is called to perform any operations necessary to restore the state of the object back to the state at which the serialization occurred. Here, the thumbnail needs to be generated, so the <code>readObject</code> method is overridden so that the thumbnail will be generated by calling the <code>generateThumbnail</code> method.</p> <p>For additional information, the <a href="http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html" rel="noreferrer">Discover the secrets of the Java Serialization API</a> article (which was originally available on the Sun Developer Network) has a section which discusses the use of and presents a scenario where the <code>transient</code> keyword is used to prevent serialization of certain fields.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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