Use of Serializable other than Writing& Reading object to/from File
Asked Answered
A

7

24

In Which Cases it is a good coding practice to use implements serializable other than Writing & Reading object to/from file.In a project i went through code. A class using implements serializable even if in that class/project no any Writing/Reading objects to/from file?

Anglicism answered 20/7, 2015 at 18:51 Comment(5)
Just be aware that implementing Serializable is not as straightforward as it might seem. It breaks encapsulation, represents an extralinguistic mechanism for object creation, and can cause security vulnerabilities. It causes implementation details that would otherwise be hidden to be exported as part of the class's API. For certain classes, great care must be used in order to develop a good serialized form.Fancyfree
What is the purpose of the serializable class that you mention?Successive
@jaco0646<br> The class about which i am talking is a pogo class.and its implements serializable. and in a different class we are setting some values in the pogo class field using different service.Anglicism
Is RMI used in your project?Adagio
@yadav..Yes we are calling some other serviceAnglicism
S
22

If the object leaves the JVM it was created in, the class should implement Serializable.

Serialization is a method by which an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.

After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

This is the main purpose of de-serialization. To get the object information, object type, variable type information from a written(loosely speaking) representation of an object. And hence serialization is required in the first place, to make this possible.

So, whenever, your object has a possibility of leaving the JVM, the program is being executed in, you should make the class, implement Serializable.

Reading/Writing objects into files (Memory), or passing an object over internet or any other type of connection. Whenever the object, leaves the JVM it was created in, it should implement Serializable, so that it can be serialized and deserialized for recognition once it enters back into another/same JVM.

Many good reads at :

Scheffler answered 20/7, 2015 at 19:3 Comment(2)
But in my case there is no any possibility that object is going to leave jvm.......:(Anglicism
Then there is no need to implement the Serializable interface. :) I hope I could explain why it is needed.Scheffler
P
10

Benefits of serialization:

  1. To persist data for future use.

  2. To send data to a remote computer using client/server Java technologies like RMI , socket programming etc.

  3. To flatten an object into array of bytes in memory.

  4. To send objects between the servers in a cluster.

  5. To exchange data between applets and servlets.

  6. To store user session in Web applications

  7. To activate/passivate enterprise java beans.

You can refer to this article for more details.

Philanthropy answered 21/7, 2015 at 14:15 Comment(0)
G
6

If you ever expect your object to be used as data in a RMI setting, they should be serializable, as RMI either needs objects Serializable (if they are to be serialized and sent to the remote side) or to be a UnicastRemoteObject if you need a remote reference.

Glossotomy answered 20/7, 2015 at 18:55 Comment(0)
T
5

In earlier versions of java (before java 5) marker interfaces were good way to declare meta data but currently we having annotation which are more powerful to declare meta data for classes.

Annotation provides the very flexible and dynamic capability and we can provide the configuration for annotation meta deta that either we want to send that information in byte code or at run time.

Here If you are not willing to read & write object then there is one purpose left of serialization is, declare metadata for class and if you are goint to declare meta data for class then personally I suggest you don't use serialization just go for annotation.

Annotation is better choice than marker interface and JUnit is a perfect example of using Annotation e.g. @Test for specifying a Test Class. Same can also be achieved by using Test marker interface.

There is one more example which indicate that Annotations are better choice @ThreadSafe looks lot better than implementing ThraedSafe marker interface.

Tarkany answered 8/8, 2015 at 5:35 Comment(0)
M
2

There are other cases in which you want to send an object by value instead of by reference:

  • Sending objects over the network.

Can't really send objects by reference here.

  • Multithreading, particularly in Android

Android uses Serializable/Parcelable to send information between Activities. It has something to do with memory mapping and multithreading. I don't really understand this though.

Mines answered 20/7, 2015 at 18:59 Comment(0)
F
1

Along with Martin C's answer I want to add that - if you use Serializable then you can easily load your Object graph to memory. For example you have a Student class which have a Deportment. So if you serialize your Student then the Department also be saved. Moreover it also allow you -

1. to rename variables in a serialized class while maintaining backwards-compatibility.
2. to access data from deleted fields in a new version (in other words, change the internal representation of your data while maintaining backwards-compatibility).

Fante answered 20/7, 2015 at 19:1 Comment(0)
E
0

Some frameworks/environments might depend upon data objects being serializable. For example in J2EE, the HttpSession attributes must be serializable in order to benefit from Session Persistence. Also RMI and other dark ages artifacts use serialization.

Therefore, though you might not immediately need your data objects to be serializable, it might make sense to declare Serializable just in case (It is almost free, unless you need to go through the pain of declaring readObject/writeObject methods)

Eusebiaeusebio answered 31/7, 2015 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.