Showing posts with label Java Serialization. Show all posts
Showing posts with label Java Serialization. Show all posts

WriteReplace method concept during Serialization in Java


WriteReplace method use by JVM during Serialization  to indentify the which object user want to searlize .

Below program for better understanding.
package Serlialization;

import java.io.Serializable;

class employee1  implements Serializable
{
      public String sname="king";
      int a;
      employee1()
      {
           
            System.out.println("superclas constructor run");
      }
      public void getNameEmployee1()
      {
            System.out.println("The employee1 getName Employee1");
      }
      /* public  Object readResolve() {
                System.out.println("Read Resolve method calling");
              throw new RuntimeException();
          }*/
     
}

public class employee  implements Serializable
{
      employee1 e= new employee1();
      String name="kumudg";
      int a;
      int c;
      transient int d;
      transient String password="password";
    static String age="raju";
    private Object writeReplace() {
        //System.out.println("The control in writeReplace method");
    return e;
      //return new SerializationProxy(this);
    }

   /*public  Object readResolve() {
        throw new RuntimeException();
    }*/

  public employee()
 
  {
        System.out.println("Inside the employee object contructor ");
  }
 
 
  public void getName()
  {
        System.out.println("Inside the getName method"+name);
       
  }
     
 
 
 
 
}


Serliaztion Test class
package Serlialization;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class serializationTest {
public static void main(String [] args)
{
                serializationTest st= new serializationTest();
                employee e= new employee();
                //employee e=null;
                 st.serializedObject(e);
                //employee de=st.deserializedObject();
                //System.out.println("The value is "+de.name + de.age);
                //System.out.println("The transient value is "+de.password);
    //System.out.println("The transient value is "+de.sname);
               
}


public void serializedObject(employee e)
{
                try {
                                FileOutputStream out= new FileOutputStream("c://ser//ser.txt");
                                ObjectOutputStream outs= new ObjectOutputStream(out);
                                outs.writeObject(e);
                               
                } catch (FileNotFoundException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                }
                catch (Exception e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                }
               
}

}
When programmer will serialize object then employee1 class’s object will searlize because the object return by WriteReplace method in Employee class returning object of Employee1