2 Replies - 213 Views - Last Post: 19 March 2012 - 09:09 PM Rate Topic: -----

#1 arianule  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-March 12

Object serialization query

Posted 19 March 2012 - 08:03 AM

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){}



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){}


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 Serialized object in a different program.

kind regards

Is This A Good Question/Topic? 0
  • +

Replies To: Object serialization query

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Object serialization query

Posted 19 March 2012 - 09:22 AM

Quote

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


That's right. If you're going to refer to it directly in your code like that, it will have to be in the classpath, just like any other class. The only way to avoid that is to load and use it via reflection
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Object serialization query

Posted 19 March 2012 - 09:09 PM

If you write both the Client and the Server it is easy to make the class Mammal available to both
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1