Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my class not serializable?

I have the following class:

import java.awt.Color;
import java.util.Vector;

public class MyClass {

    private ImageSignature imageSignature;

    private class ImageSignature implements Serializable {
        private static final long serialVersionUID = -6552319171850636836L;
        private Vector<Color> colors = new Vector<Color>();

        public void addColor(Color color) {
            colors.add(color);
        }

        public Vector<Color> getColors() {
            return colors;
        }
    }

    // Will be called after imageSignature was set, obviously
    public String getImageSignature() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(imageSignature);
        oos.close();
        return String(Base64Coder.encode(baos.toByteArray()));
    }
}

When I try to call getImageSignature(), I get an NotSerializableException - Why is that? All members are serializable, so why do I get that error?

like image 792
F.P Avatar asked Feb 23 '12 10:02

F.P


People also ask

What makes a class not serializable?

By default, classes are not serializable unless they are marked with SerializableAttribute. During the serialization process all the public and private fields of a class are serialized by default.

How do I make my class serializable?

The easiest way to make a class serializable is to mark it with the SerializableAttribute as follows. The following code example shows how an instance of this class can be serialized to a file. MyObject obj = new MyObject(); obj. n1 = 1; obj.

Are all classes serializable by default?

It's a little bit philosophical but by convention the default type of a class is not serializable, unless you explicitely define "this class can be serialized". Save this answer.


2 Answers

Every instance of ImageSignature has an implicit reference to the enclosing instance of MyClass, and MyClass is not serializable.

Either make MyClass serializable, or declare ImageSignature static:

private static class ImageSignature implements Serializable {
like image 88
NPE Avatar answered Oct 25 '22 05:10

NPE


Check the below info from Java Serialization Spec:

Note - Serialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged for several reasons. Because inner classes declared in non-static contexts contain implicit non-transient references to enclosing class instances, serializing such an inner class instance will result in serialization of its associated outer class instance as well. Synthetic fields generated by javac (or other JavaTM compilers) to implement inner classes are implementation dependent and may vary between compilers; differences in such fields can disrupt compatibility as well as result in conflicting default serialVersionUID values. The names assigned to local and anonymous inner classes are also implementation dependent and may differ between compilers. Since inner classes cannot declare static members other than compile-time constant fields, they cannot use the serialPersistentFields mechanism to designate serializable fields. Finally, because inner classes associated with outer instances do not have zero-argument constructors (constructors of such inner classes implicitly accept the enclosing instance as a prepended parameter), they cannot implement Externalizable. None of the issues listed above, however, apply to static member classes.

like image 41
bchetty Avatar answered Oct 25 '22 05:10

bchetty