Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

A Serialization Question

Experts,

I have a class named Address which has NOT implemented the Serializable interface. Then I have another class Employee which has implemented the Serializable interface. This Employee class HAS-A Address object embedded as a data member. I do the serialization. I can see the file employee1.dat is created but no data. Why the serialization is not happenning?. Here is the sample program.

prain.


import java.io.*;

class Address
{
     private String address;

     public Address(){}

     public Address( String a )
     {
          setAddress( a );
     }

     public void setAddress( String a )
     {
          address = a ;
     }

     public String getAddress()
     {
          return address;
     }

     public String toString()
     {
          return address;
     }
}

class Employee implements Serializable
{
     String name;
     Address addr;

     public Employee( String a, String b )
     {
          name = a;
          addr = new Address( b );
     }

     public String toString()
     {
          return "Employee: " + name + addr.toString();
     }

     private void writeObject( ObjectOutputStream objout ) throws IOException
     {
          try
          {
               objout.writeObject( addr.getAddress() );
               objout.defaultWriteObject();
          }

          catch( Throwable ex ){}
     }

     private void readObject( ObjectInputStream objin ) throws IOException
     {
          try
          {
               addr.setAddress( (String)objin.readObject() );
               objin.defaultReadObject();
          }

          catch( Throwable ex ){}
     }

     public static void main( String[] args )
     {
          Employee emp = new Employee( "Jack", "Bay Area" );

          try
          {
               ObjectOutputStream out = new ObjectOutputStream( new
BufferedOutputStream( new FileOutputStream( "employee.dat" )));
               out.writeObject( emp );

               out.close();

          }

          catch( Exception e ){}

          try
          {
               ObjectInputStream in = new ObjectInputStream( new
BufferedInputStream( new FileInputStream( "employee.dat" )));

               Employee emp1 = (Employee)in.readObject();
               System.out.println( emp1 );

          }

          catch( Exception e ){}

     }
}



ASKER CERTIFIED SOLUTION
Avatar of rjackman
rjackman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of superschlonz
superschlonz

There are several bugs in class Emplyeee:

1. make the fields transient which should not be serialized:

transient Address addr;

2. call defaultWriteObject() and defaultReadObject() before writing/reading
other data.

3. In readObject) method the address is null when you want to call setAddress(),
you have to allocate a new object.

addr = new Address( (String)objin.readObject() );

4. why do you make this tray {} catch( Throwable 9 {} without doing something when
it fails. This way you won't find out that something failed reading or writing.
My code is not supposed to be a Java bean, but if your read the specification of the Java Bean components there are some restrictions for working with the default serialization process like :
in order to be serializable an java Bean must provide :
- implements Serializable or Externalizable (this only if you implement yourself the serialization) interfaces
- an default constructor which takes no parameters.
- pairs of methods getFoo...&setFoo... for the serializable fields;
- static and final members are not serializable;
- all nonserializable fields must be declared as transient.
My code is not supposed to be a Java bean, but if your read the specification of the Java Bean components there are some restrictions for working with the default serialization process like :
in order to be serializable an java Bean must provide :
- implements Serializable or Externalizable (this only if you implement yourself the serialization) interfaces
- an default constructor which takes no parameters.
- pairs of methods getFoo...&setFoo... for the serializable fields;
- static and final members are not serializable;
- all nonserializable fields must be declared as transient.
This question appears to have been abandoned. In fact, I will include links to ALL your open questions

below:

Your options are:
1.  Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you

wish to do this, and allow for Expert response.
3.  Ask Community Support to help split points between participating experts, or just comment here with

details and we'll respond with the process.
4.  Delete the question. Again, you must tell the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below, include the question

QID/link.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please click this Help Desk link for Member Guidelines, Member Agreement and the Question/Answer process:

Click you Member Profile to view your question history and keep them all current with updates as the

collaboration effort continues.
https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp


PLEASE DO NOT AWARD THE POINTS TO ME.  

------------>  EXPERTS:

Please leave any comments regarding this question here on closing recommendations if this item remains

inactive another three days.

Thank you everyone.

Open Qs:
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11862698
https://www.experts-exchange.com/jsp/qShow.jsp?qid=11874078
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20059847
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20138628
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20158216
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20158215
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20167637
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20167641
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20270929


Tuvok
Moderator @ Experts Exchange

P.S.  For year 2000 question, special attention is needed to ensure the first correct response is awarded,

since they are not in the comment date order, but rather in Member ID order.

prain:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[points to Ovi]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.