Attributes modified by static and transient in java cannot be serialized. statictransient

Source: Internet
Author: User

Attributes modified by static and transient in java cannot be serialized. statictransient
Related webpage: Java serialization advanced understanding of the future (verified) Class Student1package test; import java. io. serializable; public class Student1 implements Serializable {private static final long serialVersionUID = 1L; private String name; private transient String password; private static int count = 0; public Student1 (String name, String p Assword) {System. out. println ("Call the construction method with parameters of Student"); this. name = name; this. password = password; count ++;} public String toString () {return "count:" + 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 ObjectSe RTest1 {public static void main (String args []) {try {FileOutputStream fos = new FileOutputStream ("test. obj "); ObjectOutputStream oos = new ObjectOutputStream (fos); Student1 s1 = new Student1 (" zhangsan "," 123456 "); Student1 s2 = new Student1 (" Wang Wu ", "56"); oos. writeObject (s1); oos. writeObject (s2); oos. close (); FileInputStream FCM = new FileInputStream ("test. obj "); ObjectInputStream ois = new ObjectInputStre Am (FCM); Student1 s3 = (Student1) ois. readObject (); Student1 s4 = (Student1) ois. readObject (); System. out. println (s3); System. out. println (s4); ois. close ();} catch (IOException e) {e. printStackTrace ();} catch (ClassNotFoundException e1) {e1.printStackTrace () ;}} running result: number of Student constructor calls Student's constructor: 2 Name: password: null: 2 Name: Wang Wu password: null Test1package test; import java. io. fileInputStream; import jav A. io. fileOutputStream; import java. io. IOException; import java. io. objectInputStream; import java. io. objectOutputStream; public class Test1 {public static void main (String args []) {try {FileInputStream FD = new FileInputStream ("test. obj "); ObjectInputStream ois = new ObjectInputStream (FCM); Student1 s3 = (Student1) ois. readObject (); Student1 s4 = (Student1) ois. readObject (); System. out. println (s3 ); System. out. println (s4); ois. close ();} catch (IOException e) {e. printStackTrace ();} catch (ClassNotFoundException e1) {e1.printStackTrace () ;}} running result: Number of people: 0 name: Zhang San password: null Number of people: 0 name: Wang Wu password: the execution result of the null ObjectSerTest1 class shows count = 2, which seems to be serialized, but the execution result of the class Test1 shows that count = 0 is not serialized." Serialization stores the state of the object and the status of the number of static variables. Therefore, serialization does not save static variables. This cannot be serialized. It means that the serialization information does not contain this static member domain ObjectSerTest1 and the test is successful because it is on the same machine (and the same process ), this jvm has loaded the count, so what you get is the loaded count. If you upload it to another machine or you close the program and write a program to read it into test. obj. In this case, because other machines or new processes reload the count, the count information is the initial information. "----- The result from reading test. obj from the reference page rewriting class Test1 is the initial information of count, and the above paragraph is also verified. Finally, the static and transient modified attributes of the Java object 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.