java 对象序列化 可以保存static成员,为什么?
importjava.io.*;publicclassObjectSerialize{publicstaticvoidmain(String[]args)throwsEx...
import java.io.*; public class ObjectSerialize { public static void main(String[] args) throws Exception{ ObjectA oa1=new ObjectA("测试",1,2,3); FileOutputStream fos=new FileOutputStream("fs.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(oa1); oos.close(); FileInputStream fs=new FileInputStream("fs.txt"); ObjectInputStream ois=new ObjectInputStream(fs); ObjectA oa3=(ObjectA)ois.readObject(); System.out.println(oa3.name+","+oa3.a+","+oa3.b+","+oa3.c); oa3.fn1(); } } class ObjectA implements Serializable{ String name; int a; static int b; transient int c; public ObjectA (String name,int a,int b,int c){ this.name=name; this.a=a; this.b=b; this.c=c; } void fn1(){ System.out.println("fn1"); } }输出结果是:测试,1,2,0 fn1 证明static int b序列化时被保存了。
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询