Arianule
Arianule

Reputation: 9053

Object serialization query

I am currently learning about object serialization in Java and in my understanding it is even possible to serialize an object that implements the Serializable interface and pass it through a socket to a different program.

  class Mammal implements Serializable
{
    int legs = 4;

}

public class ObjectSerial
{



    public static void main(String[] args)
    {
        try
        {
            FileOutputStream fo = new FileOutputStream("mammal.obj");
            ObjectOutputStream oo = new ObjectOutputStream(fo);

            Mammal m = new Mammal();
            oo.writeObject(m);
            oo.close();
        }
        catch(IOException e){}

        //read object
        try
        {
            FileInputStream fi  = new FileInputStream("mammal.obj");
            ObjectInputStream oo = new ObjectInputStream(fi);

            Mammal m = (Mammal)oo.readObject();
            System.out.println(m.legs);
        }
        catch(IOException e){}
        catch(ClassNotFoundException cnf){}//this exception must also be caught
    }
}

What puzzles me is when I want to retrieve the class members on the Server side for example, how would I 'reach; the Serialized object.

try
    {
        FileInputStream fi  = new FileInputStream("mammal.obj");
        ObjectInputStream oo = new ObjectInputStream(fi);

        Mammal m = (Mammal)oo.readObject();
        System.out.println(m.legs);
    }
    catch(IOException e){}
    catch(ClassNotFoundException cnf){}//t

In other words. In a different program the compiler will tell me that the symbol cannot be found.

Hope this question doesn't sound very ignorant. Just to confirm...how do i access the variables of the Serializedobject in a different program.

Regards

Upvotes: 2

Views: 253

Answers (3)

Matt
Matt

Reputation: 11805

Also, don't forget to add this:

// Better to let the compiler generate this at first
private static final long serialVersionUID = -1111111L;

And make sure it gets changed every time you change data definitions in the object (ie, add/remove/modify non-transient field definitions).

When serialized objects are written, the serialVersionUID is part of what is written. When it's read back in, it takes that serialVersionUID and compares it against the serialVersionUID for that class in the ClassLoader.

If the two don't match, it means that serialized object was generated from a different version of the class than is currently loaded, and an exception will be thrown.

Upvotes: 1

Churk
Churk

Reputation: 4637

Hopefully this site helps you a bit: http://www.javapractices.com/topic/TopicAction.do?Id=45

I was a bit wrong on not able to cast it to the original object, But you need to have the readObject method to recreate the object, which implementing serialized should have forces you to do on you Mamual Object.

Upvotes: 0

Mike Hogan
Mike Hogan

Reputation: 10623

when you are de-serializing your "mammal.obj" on the server, you will need to have Mammal.class on the classpath. And also any objects Mammal depends on also on the classpath. That is the only way, and a downside of serialization. Its couples your client and server in a binary way.

Upvotes: 3

Related Questions