user1582269
user1582269

Reputation: 305

Serializing and deserializing the object in different jvm instances

I have query that I was going through serialization in java please let me know if I open two instances of JVM , let say I open two different work spaces of eclipse in two different locations and in one work-space I have created the program to serialize the object in file having .ser extension and through another workspace I have created the program to deserialize the object by reading that .ser file .

Now please advise will that object get deserialized in different instance of JVM? The question revolves around the fact that is object serialized and deserialized in the same instance of JVM is compulsorily one..!!

Upvotes: 0

Views: 5807

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328770

The Java serialization format is standardized. Any JVM can write and read it.

The byte streams might be slightly different (for example, serializers might write some objects in a different order; if you have two int fields in a class, it doesn't matter in which order they are written).

But the result of deserializing will always be the same unless you changed the Java code of the classes that are stored in the byte stream.

Upvotes: 1

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

Serialization is a process by which we can store the state of an object into any storage medium. We can store the state of the object into a file, into a database table etc. Deserialization is the opposite process of serialization where we retrieve the object back from the storage medium.

Eg1: Assume you have a Java bean object and its variables are having some values. Now you want to store this object into a file or into a database table. This can be achieved using serialization. Now you can retrieve this object again from the file or database at any point of time when you need it. This can be achieved using deserialization.

**Serialization Example:**

Employee e = new Employee();
      e.name = "Reyan Ali";
      e.address = "Phokka Kuan, Ambehta Peer";

          e.SSN = 11122333;
          e.number = 101;
          try
          {
             FileOutputStream fileOut =
             new FileOutputStream("employee.ser");
             ObjectOutputStream out =
                                new ObjectOutputStream(fileOut);
             out.writeObject(e);
             out.close();
              fileOut.close();
          }catch(IOException i)
          {
              i.printStackTrace();
          }

**Deserializing an Object:**
The following DeserializeDemo program deserializes the Employee object created in the SerializeDemo program. Study the program and try to determine its output:



Employee e = null;
         try
         {
            FileInputStream fileIn =
                          new FileInputStream("employee.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Employee) in.readObject();
            in.close();
            fileIn.close();
        }catch(IOException i)
        {
            i.printStackTrace();
            return;
        }catch(ClassNotFoundException c)
        {
            System.out.println(.Employee class not found.);
            c.printStackTrace();
            return;
        }
        System.out.println("Deserialized Employee...");
        System.out.println("Name: " + e.name);
        System.out.println("Address: " + e.address);
        System.out.println("SSN: " + e.SSN);
        System.out.println("Number: " + e.number);

Upvotes: 1

Related Questions