开发者

I'm not sure if I understand how serializable in java works

开发者 https://www.devze.com 2023-04-06 01:34 出处:网络
I underst开发者_如何学Goand that it just saves the state of an object, but in what classes should I implement this interface?

I underst开发者_如何学Goand that it just saves the state of an object, but in what classes should I implement this interface?

For example, suppose that you have 4 classes A, B, C, D:

abstract class A { ... }
class B extends A { ... }
class C extends A { ... }

D is the class where the objects of A and B are created and manipulated:

class D { A a; B b; ... }

if I want to store the state of the program, should I say implement the Serializable interface only in D and A classes?

Also suppose that there's this class E that is just being used in order to help some calculations in D.

Should E also implement Serializable? it doesn't seem correct to me, because it's just a class that helps with calculations and it doesn't store anything of value that needs to be known at a later state.


Only serialize the things that hold data you need to get back. In this case, A seems to be the likely candidate.

I wouldn't serialize D since it is simply an aggregate of persisted objects... I would allow a method that lets me retrieve all instances of A and its children (getData()) in an array or something that lets me store everything. Then another method that lets me rebuild D with an array of A.

Also since E doesn't hold any data, it does not need to be serialize (does not have any runtime data that needs to be persisted).

B and C will inherit serializable from A.

Just ask yourself, if I restarted my app, what data I do want to have when I turn it back on. Try to make your serialized objects as lightweight as possible, and you'll make your life easier.


The idea of serialization is that you want to re-create instance later or on different machine. Maybe it helps to think about what do we need to re-create, what can be created locally.

abstract class A implements Serializable { private int a; }
abstract class B extends A { private int b; B(int b) { this.b=b;} }
abstract class C extends A { private int c; C(int c) { this.c=c;} }

This allows to serialize/deserialize instances of B and C.

class D implements Serializable {
   B someB;
   C someC;
   D(int b, int c) {
     someB = new B(b);
     someC = new C(c);
   }
}

In this case D has to serializable because we want a D instance with exactly the same B and C objects.

If E just provides methods, then we might don't need to serialize it - unless D references an instance of E and we need it to re-create a valid D instance.


Every object which you intend to serialize should implement Serializable interface. In your the class A and D needs to implement it. You need not serialize E as it does not have any state.

In generally all the classes which hold your state (has any instance level variable roughly speaking) should directly or indirectly implement Serializable if they are to be Serialized.

0

精彩评论

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