Serialization and Transient in java
if you are not familiar with what serialization is ..
Why serialization?
(official link: http://java.sun.com/developer/technicalArticles/Programming/serialization/)
We all know the Java platform allows us to create reusable objects in memory. However, all of those objects exist only as long as the Java virtual machine remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn't it? Well, with object serialization, you can flatten your objects and reuse them in powerful ways.
here read this
What is Object Serialization ?
Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.
---> Its a mechanism provided by java where the object can be represented as a sequence of bytes that includes the objects data as well as information about the objects type and types of data stored in object.
---> After serialized object has been written to a file( persistence), it can be read from the file and deserialized(de-serialization), i.e the type of information and bytes that represent the object and its data can be used to recreate the object in memory.
Interesting Fact: Most impressive is that this entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.
---> So, here the serialization of the object makes its state persistent(meaning stored in persistent storage e.g.: hard drive for later use. if not persistent , its data will be lost). That means the state of the object is converted into stream of bytes and stored in a file.
--> For Object persistence to be possible, the class needs to implement Serializable Interface( this interface has no methods to be implemented hence its called marker interface.
NOTE: For a class to be serializable successfully, 2 things should be met :
1) The class must implement java.io.Serializable interface and
2) All fields in class must be serializable. if a field is not serializable, it must be marked transient
Now what is transient?
transient:
Some times you may not want some of your members to be saved as part of object state( such data is not persisted and is lost). In that case, you declare them transient so that while serialization takes place, all other members except this member will not get serialized. Also Static members are not part of the object so they won't be serialized as well.

















