Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

readObject for ArrayList not calling constructor of objects in array

849521Apr 22 2011 — edited Apr 24 2011
I'm using externalizable for my app; the objects involved have ArrayList(s) of nested objects. All objects involved implement Externalizable. However, the constructor for the objects referenced from the ArrayList(s) are not called; nor is readExternal for such objects. This is causing problems for version control.

Example:
class X implements Externalizable
{
  static final long serialVersionUID = 1L;
  int i;
  ArrayList<Y> list;

  ... constructor, other code ...

  public void writeExternal (ObjectOutput out)
  {
    out.writeLong (serialVersionUID);
    out.writeInt (i);
    out.writeObject (list);
  }

  public void readExternal (ObjectInput in)
  {
    long UID = in.readLong();
    if (UID != serialVersionUID)
      throw new InvalidClassException (UID);
    i  = in.readInt();
    list = (ArrayList<Y>)in.readObject();
  }
}

class Y implements Externalizable
{
  static final long serialVersionUID = 1L;
  int j;

  ... constructor, other code ...

  public void writeExternal (ObjectOutput out)
  {
    out.writeLong (serialVersionUID);
    out.writeInt (j);
  }

  public void readExternal (ObjectInput in)
  {
    long UID = in.readLong();
    if (UID != serialVersionUID)
      throw new InvalidClassException (UID);
    j  = in.readInt();
  }
}
In this example, the constructor nor readExternal are called when X.readExternal executes.

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 22 2011
Added on Apr 22 2011
21 comments
785 views