3

If we need to transfer an Object across network we make the class implements Serializable . Now my question is what difference does it make?How it all works?

user3506790
  • 49
  • 1
  • 4
  • 1
    http://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html did you check the docs? – kviiri Jul 09 '14 at 09:11
  • 2
    Across network : meaning ? – Ninad Pingale Jul 09 '14 at 09:13
  • 1
    ^ I have the same question. If you're trying to send information over a network, you're not exactly going to send a Java object. Rather, you would, for instance, create a JSON document from your object and its fields, and send that. – u3l Jul 09 '14 at 09:19
  • 2
    @Trust Not necessarily. Text-based serialization (such as JSON or XML) is useful for cross-language coordination, or to decouple clients and servers. But binary serialization (such as a Java object -- albeit not necessarily Java's built-in serialization) may be preferable for other types of network communication (e.g., distributed cache being the most obvious (coherence, hazelcast), but remote agents are another classic example.) – michael Jul 09 '14 at 09:48
  • Interesting, I wasn't aware of that. My bad. – u3l Jul 09 '14 at 09:55

2 Answers2

1

In brief, There are cases when you want to transfer an object but want not to transfer all the fields of the object. Then you make the class implementing Serializable, that is a marker that any object of that class may be sent over a network. The fields inside the class that you want to to restrict from from transferring over network, mark them as transient with the keyword transient. Those fields may have get default values on other end.

You may see Serializable and transient for answer to why implement an interface in one place and keyword in other olac.

For more details see documentation by oracle on Serializable

documentation by oracle on transient

Thanks.

Community
  • 1
  • 1
karimvai
  • 319
  • 3
  • 14
1

Below paragraph describes what Serilization is (Taken from book Effective Java).

Serilization provides a framework for encoding objects as byte streams and reconstructing objects from their byte-stream encodings. Encoding an object as a byte stream is known as serializing the object; the reverse process is known as deserializing it. Once an object has been serialized, its encoding can be transmitted from one running virtual machine to another or stored on disk for later deserialization. Serialization provides the standard wire-level object representation for remote communication, and the standard persistent data format for the JavaBeans component architecture.

You can read java docs or any other standard java book(for example Head First java) to know what Serilization is and how it works.

Suresh
  • 29
  • 4