Java serialization vs. static variables

Source: Internet
Author: User

Brief introduction:

Java serialization, which means converting an object into a binary byte stream (note, not a bit stream), and then save it as a file.

Serialization operations: Saving objects to a file;

Deserialization operation: Recovering objects from a file;


Configuration:

If the object is to be serialized, the serializable interface must be integrated;

When implementing serialization, it is implemented with ObjectOutputStream;

When deserializing, it is implemented with ObjectInputStream;


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


Attention:

Java serialization cannot serialize a static variable because it holds the state of the object, and the static variable is stored in the global data area, which is generated when the object is not instantiated and belongs to the state of the class.

To facilitate understanding, we illustrate:


Example:

First, we enclose the generated classes of the objects we want to serialize:

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";p ublic string GetName () {RE Turn name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
Here we have the static variable test, which is assigned to IBM.


Let's take a 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 ();p Erson . Setage (+);p erson.setname ("YXY"); Person.test= "JAVA"; Modify the test valuefile file = new file ("C:/test.txt"); try {outputstream out = new FileOutputStream (file); objectoutput Stream 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 Objecti Nputstream (in);p erobj = (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 in the beginning, then we implemented the serialization first in code Segment 1, and then we implemented the deserialization under Code Segment 1, read the object, run, we found the result is Java, At this point, we have to ask, say do not serialize the static variable, how to read out the value of the deserialized object, test value also changed? This is precisely because we have not serialized the static variable, so it is not written to the stream, so when we want to read the value of test, it is not possible to find a new value in the deserialized file, but to go to the global data area value, because the value of the global data area is now Java, So the value read is the changed value of java.


Let's look at a piece of code here:

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); Object InputStream Objin = new ObjectInputStream (in);p erobj = (person) objin.readobject (); System.out.println (perobj.test); In.close ();} catch (IOException e) {e.printstacktrace ();} catch (ClassNotFoundException e) {e.printstacktrace ()}}}
The same is the deserialization operation, just put in another file, then run, the result is IBM, no longer the previous Java, because we did not serialize the static variable, so the value of test does not change, because when the file is run, The static variable in the global data area has not changed, so it is, as always, the original value of IBM.


Summing up, in summary, Java serialization is not serializable static variables, you must not be confused when using.



Java serialization and static variables

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.