55

I have an class defining an immutable value type that I now need to serialize. The immutability comes from the final fields which are set in the constructor. I've tried serializing, and it works (surprisingly?) - but I've no idea how.

Here's an example of the class

public class MyValueType implements Serializable
{
    private final int value;

    private transient int derivedValue;

    public MyValueType(int value)
    {
        this.value = value;
        this.derivedValue = derivedValue(value);
    }

    // getters etc...
}

Given that the class doesn't have a no arg constructor, how can it be instantiated and the final field set?

(An aside - I noticed this class particularly because IDEA wasn't generating a "no serialVersionUID" inspection warning for this class, yet successfully generated warnings for other classes that I've just made serializable.)

amaidment
  • 6,942
  • 5
  • 52
  • 88
mdma
  • 56,943
  • 12
  • 94
  • 128

3 Answers3

44

Deserialization is implemented by the JVM on a level below the basic language constructs. Specifically, it does not call any constructor.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 2
    Actually, it does call the parameterless constructor. Only classes with such a constructor are serializable. – Oak May 25 '10 at 12:42
  • 20
    @Oak. Not true. You can serialize a class without a default constructor. – Alexander Pogrebnyak May 25 '10 at 12:45
  • 4
    @Alexander: my bad. I've confused it with the fact that non-serializable superclasses to serializable types must have such a constructor. – Oak May 25 '10 at 12:48
  • 18
    @Oak: right, so Michael's answer is actually slightly inaccurate (not that it matters for the purpose of this question). Deserialization does call the constructor of the last non-serializable class in the hierarchy. – Brett Kail May 25 '10 at 13:41
25

Given that the class doesn't have a no arg constructor, how can it be instantiated and the final field set?

Some nasty black magic happens. There is a backdoor in the JVM that allows an object to be created without invoking any constructor. The fields of the new object are first initialized to their default values (false, 0, null, etc), and then the object deserialization code populates the fields with values from the object stream.

(Now that Java is open sourced, you can read the code that does this ... and weep!)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 6
    In fact, you don't even need nasty black magic, reflection (a.k.a. nasty gray magic ;) can be used to set final fields. It works as long as the values are set before the fields are read the first time. – gustafc May 25 '10 at 12:45
  • 7
    @gustafc - but black magic is needed to create the object without executing any constructors. – Stephen C May 25 '10 at 13:02
  • ... which is important because execution of any of the constructors may have unwanted side-effects. And anyhow, the >>spec<< says that no constructors are invoked in the simple case where the superclass chain is serializable. – Stephen C Jul 19 '20 at 03:41
13

Both Michael and Stephen gave you an excellent answer, I just want to caution you about transient fields.

If default value (null for references, 0 for primitives ) is not acceptable for them after deserialization then you have to provide your version of readObject and initialize it there.

    private void readObject (
            final ObjectInputStream s
        ) throws
            ClassNotFoundException,
            IOException
    {
        s.defaultReadObject( );

        // derivedValue is still 0
        this.derivedValue = derivedValue( value );
    }
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121