双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

序列化问题

lql355 lql355 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
不是说static 变量不能序列化吗?但如下程序:
import java.io.*;
public class DataObject implements Serializable
{
private static int i=0;
private String word=" ";
public void setWord(String word)
{
this.word=word;
}
public void setI(int i)
{
this.i=i;
}
public String toString()
{
return "word:"+word+";i="+i;
}
public static void main(String[] args) throws ClassNotFoundException,FileNotFoundException,IOException
{
DataObject object=new DataObject();
object.setWord("123");
object.setI(2);
System.out.println(object);
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("lql.out"));
out.writeObject(object);
ObjectInputStream in=new ObjectInputStream(new FileInputStream("lql.out"));
DataObject object2=(DataObject)in.readObject();
System.out.println(object2);
}
}
运动后:word=123;i=2;
word=123;i=2;
这如何解释呢?
请帮忙一下,谢谢!

...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
lql355
Java SE 62,621 成员 307,251 内容