Why is my class not serializable?

21,348

Solution 1

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 {

Solution 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.

Solution 3

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 }
Share:
21,348
F.P
Author by

F.P

There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors.

Updated on February 24, 2020

Comments

  • F.P
    F.P about 4 years

    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?

    • Lars
      Lars about 12 years
      Is Color of type java.awt.Color?
    • F.P
      F.P about 12 years
      java.awt.Color and java.util.Vector
    • Tomas Narros
      Tomas Narros about 12 years
      Tested your code, and it worked without any problem. Have you cleaned and rebuilt the full project before launch?
    • NPE
      NPE about 12 years
      I too cannot reproduce this. Could you provide a complete, runnable example that demonstrates the problem?
    • Mister Smith
      Mister Smith about 12 years
      Your class is serializable, just tested.
    • F.P
      F.P about 12 years
      Okay I think I made a mistake in leaving out the outer class... I updated the question, maybe now it makes more sense?
    • bchetty
      bchetty about 12 years
      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.
  • F.P
    F.P about 12 years
    Do you mean the class itself to be static or the private property of MyClass?
  • NPE
    NPE about 12 years
    @FlorianPeschka: I mean private static class ImageSignature implements Serializable {...