What is the transient keyword in Java

What is transient keyword in Java?
What is Serilization?
If you want to understand what is transient, then first learn what is serilization concept in Java if you are not familiar with that. Serilization is the process of making the object's state persistent. That means the state of the object is converted into stream of bytes and stored in a file. In the same way we can use the de-serilization concept to bring back the object's state from bytes. This is one of the important concept in Java programming because this serilization is mostly used in the networking programming. The object's which are needs to be transmitted through network has to be converted as bytes, for that purpose ever class or interface must implements Serilization interface. It is a marker interface without any methods.

What is Transient?
By default all the variables in the object is converted into the persistent. In some cases, you may want to avoid persisting some variables because you don't have the necesscity to persist those varibale. So, you can declare those variables as transient. if the variable is declared as transient, then it will not be persisted. It is the main purpose of the transient keyword.

Transient Keyword Example
Look into the following example to understand the purpose of transient keyword:


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


// output will be : First Name : SteveMiddle Name : nullLast Name : Jobs

In the above example, the variable middleName is declared as transient, so it will not be stored in the persistent storage.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值