日期:2014-05-20  浏览次数:20991 次

静态成员变量能不能被序列化????
程序如下:

import java.io.*;
class Student implements Serializable
{
  private String name;
  private transient String password;
  private static int count=0;
  
  public Student(String name,String password)
  {
  System.out.println("调用Student的带参的构造方法");
  this.name=name;
  this.password=password;
  count++;
  }
  public String toString()
  {
  return "人数: "+count+" 姓名: "+name+" 密码: "+password;
  }
}
public class ObjectSerTest1
{
public static void main(String args[])
{
try{

FileOutputStream fos=new FileOutputStream("test.obj");
ObjectOutputStream oos=new ObjectOutputStream(fos);

Student s1=new Student("张三","12345");
Student s2=new Student("王五","54321");


oos.writeObject(s1);
oos.writeObject(s2);

oos.close();

FileInputStream fis=new FileInputStream("test.obj");
ObjectInputStream ois=new ObjectInputStream(fis);

 
Student s3=(Student)ois.readObject();
Student s4=(Student)ois.readObject();
 
 
System.out.println(s3);
System.out.println(s4);
 
 
ois.close();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e1)
{
e1.printStackTrace();
}
}
}

count为Student的静态成员变量,书上说静态成员不能被序列化,那为什么此程序运行的结果:输出s3和s4时 count为2呢,按理说应该为0才对啊?还是我哪里理解错了,请高手指定,谢谢了

------解决方案--------------------
静态成员属于类级别的,所以不能序列化
这里的不能序列化的意思,是序列化信息中不包含这个静态成员域
你这个测试成功,是因为你都在同一个机器(而且是同一个进程),因为你这个jvm已经把count加载进来了,所以你获取的是加载好的count,如果你是传到另一台机器或者你关掉程序重写写个程序读入test.obj,此时因为别的机器或新的进程是重新加载count的,所以count信息就是初始时的信息。

------解决方案--------------------
同意1楼的
------解决方案--------------------
静态变量不属于对象,属于类。不能被序列化。还有瞬态的变量也不能被序列化 。

test.obj ,你打开看看 ,里面是没有
private transient String password;
private static int count=0;
这两个属性的 序列化信息的 ,输出2是因为 在当前jvm实例中count = 2 ;

你可以这样测试,写两个类来测试,一个类写入序列化文件, 另外一个类读出序列化文件


如 :

Java code

import java.io.*;

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 password) {
        System.out.println("调用Student的带参的构造方法");
        this.name = name;
        this.password = password;
        count++;
    }

    public String toString() {
        return "人数: " + count + " 姓名: " + name + " 密码: " + password;
    }
}

public class ObjectSerTest1 {
    public static void main(String args[]) {
        try {

            FileOutputStream fos = new FileOutputStream("test.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            Student1 s1 = new Student1("张三", "12345");
            Student1 s2 = new Student1("王五", "54321");

            oos.writeObject(s1);
            oos.writeObject(s2);

            oos.close();

            FileInputStream fis = new FileInputStream("test.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);

            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();
        }
    }
}