Course contentsShow
← Java
Lesson 2152 of 2,87042. SerializationFree lesson

The Public No-Arg Constructor Requirement

Learn why Externalizable classes must have a public no-argument constructor and how deserialization uses it.

The Public No-Arg Constructor Requirement

You'll learn why Externalizable classes must have a public no-argument constructor and how the deserialization process depends on it.

When you implement the Externalizable interface, Java enforces a critical requirement that doesn't exist with Serializable: your class must provide a public no-argument constructor.

Why This Requirement Exists

Unlike Serializable, which uses reflection to restore object state without calling constructors, Externalizable takes a different approach. During deserialization, Java first creates a "blank" instance of your class by calling its no-arg constructor, then immediately invokes readExternal to populate the object with data.

Think of it like assembling furniture: Serializable is like getting a pre-assembled piece, while Externalizable requires you to first get the empty frame (constructor), then add all the parts yourself (readExternal).

The Deserialization Flow

// During deserialization of an Externalizable object:
// 1. JVM calls: new YourClass()  ← needs public no-arg constructor
// 2. JVM calls: object.readExternal(in)

Without the public no-arg constructor, deserialization will fail with an InvalidClassException.

The Rule in Practice

public class Account implements Externalizable {
    private String accountNumber;
    private double balance;
    
    // REQUIRED: public no-arg constructor
    public Account() {
        // Can be empty or initialize defaults
    }
    
    public void writeExternal(ObjectOutput out) throws IOException { /* ... */ }
    public void readExternal(ObjectInput in) throws IOException { /* ... */ }
}

Key Takeaway: Externalizable classes require a public no-argument constructor because deserialization must first create an empty instance before calling readExternal to restore the object's state—without it, deserialization fails.