static, transient-modified properties in Java cannot be serialized

Source: Internet
Author: User
Tags serialization

Related pages: Advanced knowledge of Java serialization http://www.360doc.com/content/13/0728/18/13247663_303173972.shtml the following programs are from "http://bbs.csdn.net/ topics/390155251 "(Verified) class Student1package Test; import Java.io.serializable; public class Student1 Implements serializable{     private static final long Serialversionuid = 1l;     private St Ring name;     private transient String password;     private static int count = 0;  &N Bsp       public Student1 (String name,string password) {    SYSTEM.OUT.PRINTLN (" Call Student with parameter construction method ");    this.name = name;    This.password = password;    count++;  &NB Sp  }          public String toString () {    Return "Number of people:" + Count + "Name:" + Name + "Password:" + password;     }}  class Objectsertest1 package Test; import Java.io.FileInputStream ; Import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream; public class ObjectSerTest1 {      public static void Main (String args[]) {    try{    FileOutputStream fos = new FileOutputStream ("TEST.O BJ ");    ObjectOutputStream oos = new ObjectOutputStream (FOS);         Student1 S1 = NE W Student1 ("Zhang San", "123456");    Student1 s2 = new Student1 ("Harry", "");         Oos.write Object (S1);    oos.writeobject (S2);         oos.close ();       & nbsp FileInputStream fis = new FileInputStream ("Test.obj");    ObjectInputStream ois = new ObjectInputStream (FIS); nbsp   Student1 s3 = (Student1) ois.readobject ();             student1 s4 = (Student1) Ois.readobject ();               SYSTEM.OUT.PRINTLN (S3);            &NBSP System.out.println (S4);               ois.close ();      & nbsp  } catch (IOException e) {             e.printstacktrace ();      &N Bsp &NBSP,} catch (ClassNotFoundException E1) {             e1.printstacktrace ();  &N Bsp      }     }} Run Result: Call Student with parameter construction method   Call student with parameter construction method   People: 2 Name: Zhang San password: null number: 2 Name: Harry Password:null    class Test1package Test; import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream; public class test1{     public static void Main (String args[]) {  & nbsp              Try {             FileInputStream FIS = New FileInputStream ("Test.obj");            objEctinputstream ois = new ObjectInputStream (FIS);             Student1 s3 = (Student1) o Is.readobject ();            Student1 s4 = (Student1) ois.readobject ();     &N Bsp       SYSTEM.OUT.PRINTLN (S3);            SYSTEM.OUT.PRINTLN (S4);              ois.close ();       } catch (IOException e) {    & nbsp       E.printstacktrace ();       } catch (ClassNotFoundException E1) {    &N Bsp       E1.printstacktrace ();       }   }      Run Result: 0 Name: Zhang San Password: null number: 0 Name: Harry Password:null The running results of class ObjectSerTest1 display count=2, which appears to be serialized, but the run result of class Test1 shows that count=0 is not serialized. "Serialization saves the state of the object, the number of static variables in the state of the class, so serialization does not save static variables." The non-serializable meaning here is that the serialization information does not contain this static member domain ObjectSerTest1 test succeeds because it is all on the same machine (and the same process) because the JVM has loaded count in, so you get the loaded count, If you are uploading to another machine or you turn off the program rewrite to write a program to read the Test.obj, the count information is the initial information because the other machine or the new process is reloading count. "-----from the reference pageoverriding class Test1 reads the result of Test.obj display as the initial information of count, and also validates the above paragraph. Finally, the static,transient decorated properties of the Java object cannot be serialized.

static, transient-modified properties in Java cannot be serialized

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.