会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132431个问题

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

源代码如下:

Student类

package com.realive.testSequence;

import java.io.Serializable;

/**
 * @author Realive
 * @Student.java
 * @2019年4月10日
 */
public class Student implements Serializable{
	private String name;
	private int age;
	public static String schoolName; 
	private transient String pwd;//该属性的值不被序列化 
	
	
	/**
	 * 
	 */
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * @param name
	 * @param age
	 * @param pwd
	 */
	public Student(String name, int age, String pwd) {
		super();
		this.name = name;
		this.age = age;
		this.pwd = pwd;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", pwd=" + pwd + ", schoolName="
				+schoolName +"]";
	}
	
	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;
	}
	
}

Test类

package com.realive.testSequence;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Realive
 * @Test.java
 * @2019年4月10日
 */
public class Test {
	public static void main(String[] args) {
		write();
		read();
	}
	public static void write() {
		ObjectOutputStream oos =null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("E:\\student.txt"));
			Student stu = new Student("marry",20,"888888");
			Student.schoolName = "桂柳小学";//这一行为静态成员变量赋值
			oos.writeObject(stu);
		} 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("E:\\student.txt"));
			Student stu = (Student)ois.readObject();
			System.out.println(stu);
		} catch (ClassNotFoundException | 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();
				}
			}
		}
	}
}


write();read();同时执行时,输出结果为:Student [name=marry, age=20, pwd=null, schoolName=桂柳小学]

先只执行一次write(),再只执行一次read(),输出结果为:Student [name=marry, age=20, pwd=null, schoolName=null]


请问是为什么呢?

    是否可以理解为write()方法创建的Student对象,在write()方法结束调用后,因为对象并没有消失,所以read()方法反序列化Student对象时先在内存中根据对象序列化ID(或者其他标识?)查找对象,如果没有该ID对应的对象存在时才将使用从磁盘文件中反序列化对象进来,实际上read()方法反序列化得对象还是内存中的那个Student对象,读取的是该对象对应堆中的对象信息(name、age、pwd)以及方法区常量池中的静态字段(schoolName)?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2840楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 2842楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2844楼

package Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 接收客户端消息的线程类
 */
class ChatReceive extends Thread{
    private Socket socket;

    public ChatReceive(Socket socket) {
        this.socket = socket;
    }


    @Override
    public void run() {
        this.receiveMsg();
    }
    /**
     * 实现接收客户端发送的消息
     */
    private void receiveMsg(){
        BufferedReader br=null;
        try{
            br=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while (true){
                String msg=br.readLine();
                synchronized ("abc"){
                    //把读取到的数据写入公共数据区
                    ChatRoomServer.buf="["+this.socket.getInetAddress()+"]"+msg;
                    "abc".notifyAll();

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
         
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

/**
 * 向客户端发送消息的线程类
 */
class ChatSend extends Thread{
    private Socket socket;

    public ChatSend(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        this.SendMsg();
    }
    /**
     * 将公共数据区的数据发送到客户端
     */
    private void SendMsg(){
        PrintWriter pw=null;
        try{
            pw=new PrintWriter(this.socket.getOutputStream());
            while (true){
                synchronized("abc"){
                    //让线程处于等待状态
                    "abc".wait();
                    pw.println(ChatRoomServer.buf);
                    pw.flush();

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (pw!=null){
                pw.close();
            }
            if (this.socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class ChatRoomServer {
    public static  String buf;
    public static void main(String[] args) {
        System.out.println(" 1.0");
        System.out.println("Listen at 8888");
        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(8888);
            while (true){
                Socket socket=serverSocket.accept();
                System.out.println("连接到 "+socket.getInetAddress());
                new ChatReceive(socket).start();
                new ChatSend(socket).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

老师我这里代码怎么报错,没找到问题

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2850楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637