开发者

What will happen to classCounter Variable?

开发者 https://www.devze.com 2023-03-12 11:54 出处:网络
If I serialize this object and then again deserialize and get object what will be value of classCounter?

If I serialize this object and then again deserialize and get object what will be value of classCounter?

I created 5 object classCounter = 5 what I will get in new object ?

Public Counter implements se开发者_如何学编程rializable {

 private static classCounter = 0;


Counter (){

 classCounter ++;

}

}


Well it will not affect the counter at all. More over if you transfer serialized data to jvm with different classloader it may occur that counter is less then 5.


Default constructor is not called for Serializable classes on deserialization, so classCounter++ is not called. But you have to know, that default constructor will be called for superclasses if they don't implement Serializable. See explanation here.

0

精彩评论

暂无评论...
验证码 换一张
取 消