Why is my class not serializable?
Asked Answered
C

3

8

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?

Cloris answered 23/2, 2012 at 10:34 Comment(7)
Is Color of type java.awt.Color?Photocurrent
java.awt.Color and java.util.VectorCloris
Tested your code, and it worked without any problem. Have you cleaned and rebuilt the full project before launch?Panchromatic
I too cannot reproduce this. Could you provide a complete, runnable example that demonstrates the problem?Roana
Your class is serializable, just tested.Proper
Okay I think I made a mistake in leaving out the outer class... I updated the question, maybe now it makes more sense?Cloris
Why is the class private here? Is that a inner class? If so, you have to provide sufficient information (more code or stacktrace) for us to find out what went wrong? EDIT: I didn't see the update, as I didn't refresh the page.Poleax
R
19

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 {
Roana answered 23/2, 2012 at 10:47 Comment(2)
Do you mean the class itself to be static or the private property of MyClass?Cloris
@FlorianPeschka: I mean private static class ImageSignature implements Serializable {...Roana
P
2

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.

Poleax answered 23/2, 2012 at 11:14 Comment(0)
W
1

To make class Serializable you should implement Serlizable interface that is a marker or tag interface which have no method so your class's object should seralize

private static class ImageSignature implements Serializable {// code }
Whichever answered 23/2, 2012 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.