14

Recently in an interview I was asked a question:

There are 100 properties in a Java class and I should be able to serialize only 2 of the properties. How is this possible?

Marking all the 98 properties was not the answer as it is not efficient. My answer was to carve out those properties into a separate class and make it serializable.

But I was told that, I will not be allowed to modify the structure of the class. Well, I tried to find an answer in online forums, but in vain.

1
  • 1
    I just want to point out that serialization isn't really a thing in my experience in production Java. There are much faster, nicer ways that produce input that interoperates well with other languages and is smaller in resulting size in Java. (Not to mention the whole conflating thing where objects have to be aware of their storage representation). Jun 20, 2015 at 18:18

3 Answers 3

14

If it is about few fields then you can always mark them as transient. But if you need more controlled logic in your searilization then Externalizable is the answer. You can override the serialization and deserilization process by implementing methods writeExternal and readExternal methods of Externalizable interface.

Here is small code to show how you can serialize only few fields

public class Person implements Externalizable {

    String name;
    int age;

    public Person() {  }

    Person(String name, int age) {
    this.name = name;
    this.age = age;
    }


    public void writeExternal(ObjectOutput out) throws IOException  {
    out.writeObject(name);
    //out.writeInt(age); // don't write age
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    name = (String) in.readObject(); // read only name and not age
    }
}
1
  • 1
    "But I was told that, I will not be given the implementation of the class."
    – user207421
    Jun 20, 2015 at 23:10
10

The answer is transient keyword of java. if you make a property of a class transient it will not be serialized or deserialized. For example:

private transient String nonSerializeName;
4

You could override the serialization behavior without using Externalizable interface,

you need to add following methods and do the needful there,

private void writeObject(ObjectOutputStream out) throws IOException;

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

for example class could look like this,

class Foo{
  private int property1;
  private int property2;
  ....
  private int property100;

  private void writeObject(ObjectOutputStream out) throws IOException
  {
     out.writeInt(property67);
     out.writeInt(property76);
  }

  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
  {
    property67 = in.readInt();
    property76 = in.readInt();
  }
}

Refer this for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.