This discussion is archived
8 Replies Latest reply: May 24, 2011 9:50 AM by 863823 RSS

Serializable inner classes

863823 Newbie
Currently Being Moderated
How does one deal with the serialVersionUID issue with non-static member classes? Is this the best we can do?
class A implements java.io.Serializable {
  static final long serialVersionUID = 42;
  @SuppressWarnings("serial") class B implements java.io.Serializable {...}
  ...
}
MC
  • 1. Re: Serializable inner classes
    jtahlborn Expert
    Currently Being Moderated
    personally, i would add a serialVersionUID to the inner class.
  • 2. Re: Serializable inner classes
    EJP Guru
    Currently Being Moderated
    Except that he can't because it is non-static.
  • 3. Re: Serializable inner classes
    802316 Pro
    Currently Being Moderated
    Adding the serialVersionUID to the outer class only sets it for that class.

    You will have to change your nested class to be static so you can add this field.
  • 4. Re: Serializable inner classes
    jtahlborn Expert
    Currently Being Moderated
    putting a serialVersionUID on an inner class works just fine for me. am i missing something?
  • 5. Re: Serializable inner classes
    DarrylBurke Guru Moderator
    Currently Being Moderated
    The documentation for <tt>Serializable</tt> says
    A serializable class can declare its own serialVersionUID explicitly by declaring a field named "<tt>serialVersionUID</tt>" that must be static, final, and of type <tt>long</tt>
    You can't declare a static field in a non-static inner class.

    db
  • 6. Re: Serializable inner classes
    863823 Newbie
    Currently Being Moderated
    ...and pass a reference to an A instance in the constructor of B. But that would defeat the purpose of using an inner class. It seems to me that the UID in B is useless since I'll never deserialize a B instance without deserializing an A instance, so I can piggyback on the UID of A. I think the compiler shouldn't warn me about something I cannot possibly address. I just wanted to see if others had thought of this situation. Thanks for the replies.

    MC
  • 7. Re: Serializable inner classes
    jtahlborn Expert
    Currently Being Moderated
    again, this compiles just fine for me:
    import java.io.Serializable;
    
    public class TestSer implements Serializable
    {
        private static final long serialVersionUID = 1L;
    
        class InnerSer implements Serializable
        {
            private static final long serialVersionUID = 2L;
    
            public InnerSer()
            {
            }
        }
    
        public TestSer() 
        {
        }
    }
  • 8. Re: Serializable inner classes
    863823 Newbie
    Currently Being Moderated
    How interesting. I never even tried! The language specs say:
    "inner classes cannot have static members, <em>except for static final fields that are initialized to constants</em>".
    This means that the declaration of m is valid but the declaration of n is rejected at compile-time:
    class A implements java.io.Serializable {
      static final long serialVersionUID = 42;
      class B implements java.io.Serializable {
        static final long serialVersionUID = 42;
        static final String m = "42";
        static final String n = String.valueOf(42);
      }
    }
    MC

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points