Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java have transient fields?

Why does Java have transient fields?

like image 468
Animesh Avatar asked May 26 '09 12:05

Animesh


People also ask

What are transient fields?

transient Fields: Variables may be marked transient to indicate that they are not part of the persistent state of an object. 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.

What is transient object in Java?

transient is a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes.

What are transient and automatic variables Java?

A transient variable is a special type of variable which we create by using the transient keyword. It is a special type of variable which have a non-serialized value at the time of serialization. A variable that is initialized by its default value during de-serialization is known as a transient variable.


2 Answers

The transient keyword in Java is used to indicate that a field should not be part of the serialization (which means saved, like to a file) process.

From the Java Language Specification, Java SE 7 Edition, Section 8.3.1.3. transient Fields:

Variables may be marked transient to indicate that they are not part of the persistent state of an object.

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.

Here's a GalleryImage class which contains an image and a thumbnail derived from the image:

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();     }     } 

In this example, the thumbnailImage is a thumbnail image that is generated by invoking the generateThumbnail method.

The thumbnailImage field is marked as transient, so only the original image 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.)

At the time of deserialization, the readObject 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 readObject method is overridden so that the thumbnail will be generated by calling the generateThumbnail method.

For additional information, the article Discover the secrets of the Java Serialization API (which was originally available on the Sun Developer Network) has a section which discusses the use of and presents a scenario where the transient keyword is used to prevent serialization of certain fields.

like image 124
coobird Avatar answered Sep 26 '22 06:09

coobird


Before understanding the transient keyword, one has to understand the concept of serialization. If the reader knows about serialization, please skip the first point.

What is serialization?

Serialization is the process of making the object's state persistent. That means the state of the object is converted into a stream of bytes to be used for persisting (e.g. storing bytes in a file) or transferring (e.g. sending bytes across a network). In the same way, we can use the deserialization to bring back the object's state from bytes. This is one of the important concepts in Java programming because serialization is mostly used in networking programming. The objects that need to be transmitted through the network have to be converted into bytes. For that purpose, every class or interface must implement the Serializable interface. It is a marker interface without any methods.

Now what is the transient keyword and its purpose?

By default, all of object's variables get converted into a persistent state. In some cases, you may want to avoid persisting some variables because you don't have the need to persist those variables. So you can declare those variables as transient. If the variable is declared as transient, then it will not be persisted. That is the main purpose of the transient keyword.

I want to explain the above two points with the following example (borrowed from this article):

package javabeat.samples;   import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;   class NameStore implements Serializable{     private String firstName;     private transient String middleName;     private String lastName;      public NameStore (String fName, String mName, String lName){         this.firstName = fName;         this.middleName = mName;         this.lastName = lName;     }      public String toString(){         StringBuffer sb = new StringBuffer(40);         sb.append("First Name : ");         sb.append(this.firstName);         sb.append("Middle Name : ");         sb.append(this.middleName);         sb.append("Last Name : ");         sb.append(this.lastName);         return sb.toString();     } }  public class TransientExample{     public static void main(String args[]) throws Exception {         NameStore nameStore = new NameStore("Steve", "Middle","Jobs");         ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("nameStore"));         // writing to object         o.writeObject(nameStore);         o.close();           // reading from object         ObjectInputStream in = new ObjectInputStream(new FileInputStream("nameStore"));         NameStore nameStore1 = (NameStore)in.readObject();         System.out.println(nameStore1);     } } 

And the output will be the following:

First Name : Steve Middle Name : null Last Name : Jobs 

Middle Name is declared as transient, so it will not be stored in the persistent storage.

like image 43
9 revs, 9 users 56% Avatar answered Sep 24 '22 06:09

9 revs, 9 users 56%