3

In my interview i got one question how to achive Serialization and I gave answer as

public class SerializationSample {
    public static void main(String args[]) {
        ...........
        FileOutputStream fos = new FileOutputStream(outFile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(serializableObject);
        ............
}
class SerializationBox implements Serializable {
    ............
    ............
}

but the interviewer told, this will serialize whole object and he asked 2 questions

  1. How you will write a code if i want to serialize single object?
  2. Why we use long_integer id during serialization?

Please any one can help me to finding answer for this question

Som
  • 826
  • 3
  • 10
  • 20
  • 1
    Did you ask Google about it?! I bet it would have given you answers faster. – Rahul Sep 18 '13 at 11:22
  • i didnt got Q1 in google – Som Sep 18 '13 at 11:27
  • I am not sure if he means serialization of singleton class. If that's the case its already [answered](http://stackoverflow.com/questions/3930181/how-to-deal-with-singleton-along-with-serialization) on SO – ch4nd4n Sep 18 '13 at 11:34
  • thank you a lot sir may be he will be expecting singleton answer only from me – Som Sep 18 '13 at 11:39
  • Did he really say 'long_integer id'? Or 'static long serialVersionUID'? – user207421 Sep 18 '13 at 11:48
  • 1
    And NB 'this will serialize whole object' isn't correct anyway. It will serialize the object and all its non-static non-transient members. – user207421 Oct 02 '13 at 09:34

1 Answers1

1

Let me quote an essence from similar and already answered question

The serialVersionUID represents your class version, and you should increment it if the current version of your class is not backwards compatible with its previous version.

and

automatically-generated UID is generated based on a class name, implemented interfaces, and all public and protected members. Changing any of these in any way will change the serialVersionUID. So you don't need to mess with them only if you are certain that no more than one version of the class will ever be serialized (either across processes or retrieved from storage at a later time).

I recommend you to visit that link and, as always, search the web.

Hope that helps.

Community
  • 1
  • 1
Eel Lee
  • 3,513
  • 2
  • 31
  • 49