Java serialization and static, Java serialization static

Source: Internet
Author: User

Java serialization and static, Java serialization static

Introduction:

Java serialization refers to converting an object into a binary byte stream (note, not a bit stream) and saving it as a file.

Serialization: Save the object to a file;

Deserialization: Restore an object from a file;


Configuration:

To serialize an object, you must integrate the Serializable interface;

Use ObjectOutputStream to implement serialization;

ObjectInputStream is used for deserialization;


Method:

1. serialization:

(1) public ObjectOutputStream (OutputStream out) throws IOException

(2) public final void writeObject (Object obj)

(3) public void close () throws IOException


2. deserialization:

(1) public ObjectInputStream (InputStream in) throws IOException

(2) public final void readObject (Object obj)

(3) public void close () throws IOException


Note:

Java serialization Cannot serialize static variables because they store the object state, while static variables are saved in the global data zone, which is generated when the object is not instantiated, the status of the class.

For ease of understanding, let's give an example:


Example:

First, attach the generation class of the object to be serialized:

package test;import java.io.Serializable;public class Person implements Serializable{/** *  */private static final long serialVersionUID = 1L;private String name;private int age;public static String test="IBM";public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
Here we have the static variable test, which is assigned to IBM.


Let's look at two codes:

package test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;public class Main{public static void main(String[] args){Person person = new Person();person.setAge(25);person.setName("YXY");Person.test="JAVA"; //modify the test valueFile file = new File("c:/test.txt");try {OutputStream out = new FileOutputStream(file);ObjectOutputStream objout = new ObjectOutputStream(out);objout.writeObject(person);objout.close();} catch (IOException e) {e.printStackTrace();}//code segment 1Person perobj = null;try {InputStream in = new FileInputStream(file);ObjectInputStream objin = new ObjectInputStream(in);perobj = (Person)objin.readObject();System.out.println(perobj.test);in.close();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}}
In this code, we changed the value of the static variable test to JAVA at the beginning, and then we first implemented serialization on code segment 1, next we implement deserialization, reading objects, and running under code segment 1. We found that the result is JAVA. Now you have to ask: do not serialize static variables, how does one read the value of the deserialization object? Does the value of test change? This is because we have not serialized the static variable, so it is not written into the stream, so when we want to read the value of test, it is impossible to find a new value in the deserialization file, but to go to the global data zone, because the value of the global data zone is now JAVA, therefore, the read value is the changed value in JAVA.


Next let's look at a piece of code:

package test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;public class TEST {public static void main(String[] args){File file = new File("c:/test.txt");//code segment 1Person perobj = null;try {InputStream in = new FileInputStream(file);ObjectInputStream objin = new ObjectInputStream(in);perobj = (Person)objin.readObject();System.out.println(perobj.test);in.close();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}}
It is also a deserialization operation, but it is only put into another file, and then run again, the result is IBM, no longer the previous JAVA, it is precisely because we have not serialized static variables, therefore, the value of test is not changed, because the static variable in the global data area is not changed when the file is run, so it is still its original value IBM.


To sum up, Java serialization Cannot serialize static variables, so do not confuse them when using them.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.