Serialization in java




  1. What is Serialization in Java ?
    The process of of writing state of an object to a file called Serialization. But strictly speaking, It is the process of converting object from java supported from into file file supported form or network supported form or network supported form.
    By using FileOutputstream or ObjectOutputStream we can achieve serialization.
  2. Deserialization :- The process of reading state of an object from from the file is called deserialization . But strictly speaking it is process of converting an object from either file supported form or network supported into java supported form.
  3. We can serialize  to be only serializable  object. An Object is said to be serializable if and only if the corresponding class implements Serializable interface. Serializable interface is present in the java.io package. It doesn't contain any method. Its a marker interface. If we try to serialize, non-serialized object  then it will throw NotSerialzableException.
  4. Transient Keyword
    Transient keyword is only applicable for variable not for class or method. At the time of serialization, if we don't want save the value of a particular variable to meet the security constraints then we should  declare that variable as Transient.
  5. Transient Vs Static variable
    Go through the below program and find their outputs also by changing situations




  6. What is Custom serialization in java ?
    First try to understand a scenario as given below


    We are using the below methods for serialization and deserialization respectively.
    1. private void writeObject(ObjectOutputStream obj) throws Exception // serialization
    2. private void readObject(ObjectInputStream obj) throws Exception //deserialization
    The above methods will be excuted automatically at time of serialization and deserialization. Hence at the time of serialization/deserialzation , if we want to perform any activity. we have to define that in these methods only.
    Note :- The above methods are call back methods because these are executed automatically by the JVM.
  7. While performing which object serialization we have to do extra work in the corresponding class we have to define the below methods.
    1. private void writeObject(ObjectOutputStream obj) throws Exception // serialization
    2. private void readObject(ObjectInputStream obj) throws Exception //deserialization
    For e.g.:-
    While performing "Person" object serialization we have to define above methods in the Person class.
  8. While performing custom serialization for the class "Person", We have to do below things

Comments

Popular posts from this blog

Sealed Classes and Interfaces in Java

Introduction of RabbitMQ

RabbitMQ Installation on Windows