Why is java.lang.Void not Serializable?

java serializable
implements serializable java
binary serialization in java
use of serialization in java
serializable interface in java
serializable constructor java
advantages of serialization in java
serialize object

It is possible to serialize the primitive 'void' per default, why does not the object 'Void' extend Serializable?

Added example:

The RootImplementation will have a compilation error saying "Void is not within its bound" since it does not extend Serializable. Though would 'someMethod' be declared with 'void' it would be no problem.

public interface Root<R extends Serializable> extends Serializable {
  R someMethod();

public class RootImplementation implements Root<Void> {
  public Void someMethod() {
    return null;

OK, in response to your example, no if you changed the method to void it would not work, as the method has to have a return type (even if Java now allows covariant return types in overridden methods). The discussion of void confuses the issue.

What you want to do is declare a type parameter as a "will just return null." Void is generally a good choice for that, but for Void to work, the return type has to be Object. Void can't implement every interface in the API just because someone might want to use it to indicate a null return on a type parameter.

There are three ways to look at your problem:

  1. Serializable is an overly restrictive type declaration. You should really be using Object. Do you really need it to be Serializable?
  2. You can just declare the type parameter as Serializable, and in practice return null. This dosn't fully indicate that you are returning null every time, but it may be enough.
  3. You can declare your own class called Null which implements Serializable, perhaps as a static nested class of the Root interface, and use that as the type parameter in this case. You will find making your own Null object is not that uncommon, even in the standard JDK there is (a private) one.

Serializable but no void constructor - Java queries, For JavaTM objects, the serialized form must be able to identify and verify the If the field's value is null or is otherwise not an instance of ObjectStreamField[] package java.io; public interface ObjectOutput extends DataOutput { public void� The NotSerializableException is thrown when attempting to serialize or deserialize an object that does not implement the java.io.Serializable interface. Throughout this article we’ll get into the nitty gritty of the NotSerializableException , starting with where it resides in the overall Java Exception Hierarchy .

The javadoc is clear:

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword

Because you can not use it, it does not need to be Serializable (except reflection stuff).

And for the second question: void != Void (if you are think about != in a not java expression)

Yes void is a keyword and Void a class.

Java Object Serialization Specification: 1, An ObjectStreamClass object for a Class that is not a dynamic proxy class is represented method; The ObjectStreamClass of its supertype (null if the superclass is not serializable) The ObjectStreamClass of its supertype, java. lang.reflect. The class must implement the java.io.Serializable interface. All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient. If you are curious to know if a Java Standard Class is serializable or not, check the documentation for the class.

I will put it here as comminity-wiki

Thou can (de)serialize java.lang.Void b/c you can initialize it with null only. Java doesn't care if a class implements java.io.Serializable if it's null.

Result of the code


  public class VoidOut implements java.io.Serializable{
    int x=1;
    Void v = null;

    public static void main(String[] args) throws Throwable{
        VoidOut v = new VoidOut();
        ByteArrayOutputStream b =new ByteArrayOutputStream(256);
        ObjectOutputStream o = new ObjectOutputStream(b);
        ObjectInputStream in =new ObjectInputStream(new ByteArrayInputStream(b.toByteArray()));

Java Object Serialization Specification: 6, Serialization in Java is a mechanism by which objects can be marshaled public void execute(Runnable task) throws IOException { // sends the task somewhere to be executed }. Task objects that implement both the Runnable and java.io. The Timer class is not serializable because it contains a running� We get it because, by default, the JVM associates a version number to each serializable class to control the class versioning. It is used to verify that the serialized and deserialized objects have

To quote the Javadocs:

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

Since the class is uninstantiable, it cannot be deserialized. Ergo no need for serialization support.

On Lambdas, Anonymous Classes and Serialization in Java, The default serialized form lacks any enforcement of class invariants; consequently, programs must @param s the stream */ private void readObject( java.io. In Java, ArrayList class is serializable by default. It essentially means that we do not need to implement Serializable interface explicitly in order to serialize ArrayList. We can directly use ObjectOutputStream to serialize ArrayList, and ObjectInputStream to deserialize an arraylist object.

It is possible to serialize the primitive 'void' per default, why does not the object 'Void' extend Serializable?

Void doesn't carry a value, hence it makes no sense to serialize it.

Doesn't this mean that void != Void ?

True, just as int != Integer. But when you serialize and deserialize two ints, newint==oldint (I mean int, not Integer!!!). No such construct would be possible with void. It just doesn't make any sense to serialize nothing.

SER07-J. Do not use the default serialized form for , The Class object representing the pseudo-type corresponding to the keyword void . Inherited methods. From class java.lang.Object� Serialization (C#) 01/02/2020; 4 minutes to read +4; In this article. Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file.

Void, In general a Serializable class is expected to fulfil its contract and not have an ObjectOutputStream out) throws IOException private void readObject(java.io. A serializable type is one that is marked with the System.SerializableAttribute attribute. When the type is serialized, a System.Runtime.Serialization.SerializationException exception is thrown if the type contains an instance field of a type that's not serializable and doesn't implement the System.Runtime.Serialization.ISerializable interface.

Java: Fields in a "Serializable" class should either be transient or , private transient String middleName; // transient variables will not be serialized, serialised object holds null. private String lastName;. private int� Prerequisite : – Serialization, Inheritance Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory.

Everything You Need to Know About Java Serialization Explained , To make a Java object serializable you implement the java.io. such as Thread , OutputStream and its subclasses, and Socket are not serializable. java.io. ObjectOutputStream; public class Main { public static void main(String[] args) { String� For example, you can use this attribute to prevent the serialization of sensitive data. The target objects for the NonSerializedAttribute attribute are public and private fields of a serializable class. By default, classes are not serializable unless they are marked with SerializableAttribute. During the serialization process all the public and

  • There is no such thing as a primitive void. And Void never has an instance, and I can't think of a use case where it would be a field on a class. But if it is, you can always make it transient, as it will always be null anyway.
  • What do you mean by "it is possible to serialize the primitive 'void' per default"? There's no data to serialize... can you give an example of what you mean?
  • Jon Skeet can serialize void!
  • You can serialize Void, mostly b/c you can't have an instance of the Class (Constructor.setAccessible(true) and Unsafe.allocateInstance(Void.class) don't count)
  • Side note: serializable is meant to be an implicit/tagging interface.
  • Thanks for the answer. 1. As ColinD mentions; most just strip the Serialziable of the generic type since the class declaration itself implies not to save not-Serializable objects on the implementation. Though as quite a few people are going to use the interface and make implementations, I want it to be as helpful as possible to not make an error. 2. I don't want eventual extension to be able to return anything else than void (or null in this case since Void must be used). 3. This is how I have done at the moment, though I wanted to ask the question and see if anyone had a better solution.
  • @Tomas Forsman, a class implementing serializable doesn't mean it can be serialized. Consider this: private void writeObject(ObjectOutputSteam out){throw new NotSerializableException(getClass().getName());} I do use that to make sure some extended classes never reach serialization form.