transient

What is transient variable in Java

Posted on Updated on

What is transient variable in Java

transient is a keyword in Java, which means variable is marked transient to indicate that they are not part of persistent state while Object is being saved into File system or DB.

Today we are moving toward annotation base programming rather than manual configuration and sometime we declared properties in Pojo class in which we marked few of perperties as transient then that properties only useful at application level not DB or File System.Hence we can control fields being saved during Serialization process using transient keywork.

Below example of Weather.Java indicate that temperature field value is not saved into File System or dB. Only city field value would be saved.


public class Weather {
private String city;
private transient String temperature;
--------
--------
}

Similarly, if you are familiar with MondoDB or Hibernate Annotation base Pojo class, Same way we can control the fields from being persistence state.

LoginModel.Java :- Mongodb pojo Java Class

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "Login")
public class LoginModel {
 
 @Id
 private String id;
 private String userName;
 private String password;
@Transient
private String sessionId;
-------------
-------------
 Below are complete code of transient-

Weather.Java is a Serialized Java class.

import java.io.Serializable;

public class Weather implements Serializable {
 /**
 * 
 */
 private static final long serialVersionUID = 2983365916512327738L;
 private String city;
 private transient String temperature;

 public Weather(String city, String temperature) {
 this.city = city;
 this.temperature = temperature;
 }

 public String getCity() {
 return city;
 }

 public void setCity(String city) {
 this.city = city;
 }

 public String getTemperature() {
 return temperature;
 }

 public void setTemperature(String temperature) {
 this.temperature = temperature;
 }

 @Override
 public String toString() {
 StringBuilder builder = new StringBuilder();
 builder.append("Weather [city=").append(city).append(", temperature=")
 .append(temperature).append("]");
 return builder.toString();
 }

}

 WeatherSerializationService .java : read or write Object as serialization form


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;


public class WeatherSerializationService {
	
	private static final String FILE_NAME="weather.sar";
	
	public static void main(String args[]){
		Weather weather = new Weather("Delhi","42'C");
		WeatherSerializationService weatherService = new WeatherSerializationService();
		System.out.println("Save Serializated Object "+weather);
		weatherService.save(weather);
		
		Weather readWeather = weatherService.retrive();
                if( null == readWeather ){
                    System.out.println(" Unable to read Serialization Object - Weather ");
                }
		System.out.println("Read Serializated Object "+readWeather);
		
	}
	public void save(Weather weather){
		FileOutputStream fileOutputStream = null;
		ObjectOutput objectOut = null;
		try {
			fileOutputStream = new FileOutputStream(FILE_NAME);
			objectOut = new ObjectOutputStream(fileOutputStream);
			objectOut.writeObject(weather);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(null == objectOut ) return;
                                if(null == fileOutputStream ) return;
				objectOut.close();
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	public Weather retrive(){
		ObjectInput objectInput = null;
		FileInputStream inputStream = null;
		try {
			inputStream = new FileInputStream(FILE_NAME);
			objectInput = new ObjectInputStream(inputStream);
			return (Weather)objectInput.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			try {
				if(null == objectInput ) return null;
				if(null == inputStream ) return null;
				
				objectInput.close();
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

}

Output here :-

Save Serializated Object Weather [city=Delhi, temperature=42’C]
Read Serializated Object Weather [city=Delhi, temperature=null]