Java serialization extension of class

Wouter Vandenputte :

suppose i have a class Article

public class Article implements Serializable
{
   private static final long serialVersionUID = 1420672609912364060L;
   private String title;
   private String text;
   private UUID uid;

   public Article(){ //empty default constructor
   }

   // Getters and setters 
   ...  
}

and I serialize multiple instances of it using the following code

public void save(Article article)
{
    FileOutputStream fos = null;
    try {
        fos = context.openFileOutput("article_"+ article.getUid(), Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(article);
        os.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I then later extend the class making it look like this:

public class Article implements Serializable
{
   private static final long serialVersionUID = 1420672609912364060L;
   private String title;
   private String text;
   private Person author;
   private UUID uid;

   public Article(){
       author = Person.UNKNOWN;
   }

   // Getters and setters 
   ...
}

Then when I load it in, why is author null? I always thought the default constructor was called and then the setter for every found property, but putting a breakpoint in the default constructor and then debugging shows it isn't called. Meaning that for the old Article class, the author would simply be null instead of Person.UNKNOWN. The class Person itself is also serializable.

This is my loading in code if it might be relevant.

private Article loadArticle(String fileName)
{
    FileInputStream fis = null;
    try {
        fis = context.openFileInput(fileName);
        ObjectInputStream is = new ObjectInputStream(fis);
        Article article = (Article ) is.readObject();
        is.close();
        fis.close();
        return article;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidClassException e) {
    }
    catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

Note that is all Android configuration

m.antkowicz :

I always thought the default constructor was called

Nope it wasn't. Take a look at the documentation

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130064&siteId=1