开发者

Can anything be serialized as long as it implements Serializable?

开发者 https://www.devze.com 2023-03-18 22:48 出处:网络
I\'ve already seen this: How to test in java that a class implements serializable correctly I just have a more general question. If you create an object (say a circle) like this (assume imports are t

I've already seen this: How to test in java that a class implements serializable correctly

I just have a more general question. If you create an object (say a circle) like this (assume imports are there):

class Circle implements Serializable {
     int radius;
     Circle(int radius){
         this.radius = radius;
     }

     public void updateRadius(int radius){
         this.radius = radius
     }

     public int getRadius(){
         return radius;
     }
}

Is that all that is needed for it to be serializable? Do I need to add more methods 开发者_开发问答to it for it to be serialized properly? I have a class a lot more complex, but didn't feel like posting all of it when this should provide ample example.


All of the instance variables also need to be serializable as well. This can cause problems when you're working with an external library and your "serializable" class holds a reference to a non-serializable class.


Yes, it's all you need as long as the transitive closure of all the referenced objects are all serializable.

Note this doesn't mean the deserialized object will actually work - that depends on the semantics of "Circle". The easiest way to lose out is becase the new object will be a copy, not the identical object.


Very simple answer.

Let's say you live in a certain province of good ol Canada that requires all your business communication to be conducted in French.

The name of your business is la chance. It performs debt-collection, spam-mail marketing and user tracking services.

Your parent company is la connerie. Your company does not have the facility to conduct debt-collection, spam-mail marketing and user tracking services in French. Anyway, your parent company la connerie conducts all transactions for your company anyway and they have the facility to conduct debt-collection, spam-mail marketing and user tracking services in French. So your company is safe from being raided by the provincial government authorities - no worries.

Actually la connerie does not have facilities to conduct debt-collection, spam-mail marketing and user tracking services in French because it is actually owned by a holding company du fumier. And du fumier has the facilities to conduct debt-collection, spam-mail marketing and user tracking services in French and does so for all its child entities.

So every little business entity under the du fumier umbrella can safely declare that they IMPLEMENT French debt-collection, spam-mail marketing and user tracking services without needing each of themselves the facilities to conduct such French business communication, because they depend on du fumier to do it for them.

One day, your company decided to acquire another company les merdes du chevre and their business is debt-collection. The company is newly set up and is actually an empty shell. but it is safe from the provincial French linguistic authorities because it can depend on your company to perform debt-collection in French. So les merdes du chevre can happily declare that they IMPLEMENT French debt-collection services without lifting a finger to do anything about it.

The next week, your company acquires another company les crapes des crabes whose business is selling crabby by-products. However, they do not have the facilities to sell crabby by-products in French. And because of that you will have to create a dept to IMPLEMENT selling crabby by-products in French. And failing to do so, the provincial linguistic authorities would take the EXCEPTION of hauling your company to court for not IMPLEMENTING selling crabby by-products in French.

But you can escape the prosecution/persecution by the linguistic authorities if you declare that les crapes des crabes is a TRANSIENT entity. You acquired it with intention of transferring it to british columbia and that you never had any intention to actively use that company to sell crabby by-products in the province.

0

精彩评论

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