JavaIO对象流中静态字段是无法被序列化问题

发现问题:
在测试Student类的静态字段是否可以被序列化时,如果在Test类main方法中先调用write(),再调用read()方法,静态字段schoolName可以被打印出来;但是如果先write()执行完程序,再把write()注释掉,执行read()方法,静态字段schoolName值为null。
Student类:

package Object2;

import java.io.Serializable;

public class Student implements Serializable{

private String name;

private int age;

public static String schoolName;  //学校名称

private transient String pwd;  //属性的值将不再被序列化

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;

public String getPwd() {

   return pwd;

public void setPwd(String pwd) {

   this.pwd = pwd;

public Student(String name, int age, String pwd) {

   super();

   this.name = name;

   this.age = age;

   this.pwd = pwd;

public Student() {

   super();

@Override

public String toString() {

   return "Student[name=" + name + ", age=" + age + ", pwd=" + pwd + ",schoolName=" + schoolName + "]";
}
}

测试类:



package Object2;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

public class Test {

public static void main(String[] args) {

   write();

   read();

}

public static void write() {

   ObjectOutputStream oos = null;

   try {

     oos = null;

     oos = new ObjectOutputStream(new FileOutputStream("F:\\object2.txt"));

     Student stu = new Student("hkj",19,"62536");

     Student.schoolName = "中北大学";

     oos.writeObject(stu);

   } catch (FileNotFoundException e) {

     // TODO Auto-generated
catch block

     e.printStackTrace();

   } catch (IOException e) {

     // TODO Auto-generated
catch block

     e.printStackTrace();

   }finally {

   if(oos!=null) {

     try {

        oos.close();

     } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

     }

   }

   }

}

public static void read() {

ObjectInputStream ois = null;

   try {

     ois = new ObjectInputStream(new FileInputStream("F:\\\\object2.txt"));

     Student s = (Student)ois.readObject();

     System.out.println(s);

   } catch (FileNotFoundException e) {

     // TODO Auto-generated
catch block

     e.printStackTrace();

   } catch (ClassNotFoundException e) {

     // TODO Auto-generated
catch block

     e.printStackTrace();

   } catch (IOException e) {

     // TODO Auto-generated
catch block

     e.printStackTrace();

   }finally {

   if(ois!=null) {

     try {

        ois.close();

     } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();
     }
   }
   }
}

}


问题截图:
在这里插入图片描述
问题原因:

1、write();read();同时执行时,先执行write() Student.schoolName = “中北大学”; 会在write()中加载Student类,并为schoolName进行赋值。而write();read();两个方法在同一线程中,再执行read();时打印出来schoolName的值并不属于被序列化的学生对象,所以会打印出具体的值。

2、先只执行一次write(),再只执行一次read(),两个方法并不属于同一个线程,在执行read()时Student stu = (Student)ois.readObject();是在read()中加载Student类,此时schoolName并未被赋值所以会是null。

修改代码:

public static void main(String[] args) {
 //write();
 read();
}

运行截图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44192389/article/details/100860709