JAVA partial serialization keyword transient

JAVA serialization: The serialization of an object in Java refers to the conversion of an object into a byte sequence that contains the data and information of the object. A serialized object can be written to the database or Files can also be used for network transmission. Generally, when we use cache (not enough memory space may be stored locally to the hard disk) or remotely call rpc (network transmission), we often need to make our entity classes implement the Serializable interface. Just to make it serializable.
Of course, the ultimate goal after serialization is to deserialize and restore the original Java object. Therefore, the serialized byte sequence can be restored to a Java object, and this process is deserialization.
There are often scenarios where some parameters in a class are serialized meaningless. Serialization will only increase the size of the disk or network transfer. There are some sensitive information in a class, such as passwords. Security is reduced after serialization. There is an object in a class that cannot be serialized. And this object comes from the framework or system level. At this time we can use the transient keyword. Realize partial serialization. The main function of the transient keyword is: when the object is serialized (write byte sequence to the target file), transient prevents those variables declared with this keyword in the instance from persisting; when the object is deserialized (from the source File read byte sequence for reconstruction), such instance variable values ​​will not be persisted and restored.

Example:
Here is a Company class:

public class Company implements Serializable{
	private String companyName;
	private String companyAddress;
	private String companyEmail;
	private transient List<Employee> employees;
	
	public Company(String companyName, String companyAddress, String companyEmail, List<Employee> employees) {
		this.companyName = companyName;
		this.companyAddress = companyAddress;
		this.companyEmail = companyEmail;
		this.employees = employees;
	}
}

There is an Employee class

public class Employee implements Serializable{
	private String Name;
	private String phone;
	public Employee(String name, String phone) {
		Name = name;
		this.phone = phone;
	}
}

testing method:

public class TestSerializable {

	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		Employee employee1 = new Employee("张三","1234");
		Employee employee2 = new Employee("张四","1234");
		Employee employee3 = new Employee("张五","1234");
		List<Employee> employees = new ArrayList();
		employees.add(employee1);
		employees.add(employee2);
		employees.add(employee3);
		Company company = new Company("百度","百度大厦","3333",employees);
		
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("rectangle"));
        // 往流写入对象
        o.writeObject(company);
        o.close();
        System.out.println("反序列化之前的雇员" + company.getEmployees());
        // 从流读取对象
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("rectangle"));
        Company company1 = (Company)in.readObject();
        
        System.out.println("反序列化之后的雇员" + company1.getEmployees());
	}

}

Test Results
We all know if Employee does not implement Serializable. When serializing Company in this way, an exception java.io.NotSerializableException will be thrown. This is because the variable Employee in Company cannot be serialized. But using transient can achieve partial serialization.

Guess you like

Origin blog.csdn.net/xu_coding/article/details/86654524
Recommended