JavaIO 개체 스트림의 정적 필드를 직렬화 할 수 없습니다.

문제 발견 :
Student 클래스의 static 필드를 직렬화 할 수 있는지 테스트 할 때 Test 클래스의 main 메서드에서 write ()를 호출 한 다음 read () 메서드를 호출하면 static 필드 schoolName이 인쇄 될 수 있지만 write (first) ) 프로그램 실행 후 write ()를 주석 처리하고 read () 메서드를 실행하면 static 필드 schoolName 값은 null입니다.
학생 수업 :

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 = "North University of China"; Student 클래스는 write ()에로드되고 schoolName이 할당됩니다. write (); read (); 두 메서드가 같은 스레드에 있고 read (); 다시 실행하면 출력 된 schoolName의 값이 직렬화 된 student 객체에 속하지 않으므로 특정 값이 인쇄됩니다.

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