静态成员变量能不能被序列化????

oyhj_nicholas 2012-07-30 04:01:34
程序如下:

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才对啊?还是我哪里理解错了,请高手指定,谢谢了
...全文
2003 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
游一游走一走 2012-07-30
  • 打赏
  • 举报
回复
执行下下面的代码就可以推翻你的想法了,static不序列化的

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");
System.out.println(s1);
System.out.println(s2);

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

oos.close();

new Student("xyq","i love you");

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();
}
}
}
五哥 2012-07-30
  • 打赏
  • 举报
回复
静态变量不属于对象,属于类。不能被序列化。还有瞬态的变量也不能被序列化 。

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

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


如 :


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




import java.io.FileInputStream;
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[]){

try {

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



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

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧