Statically serializable



  • Trying to clean up the scary Java codebase we enherited, I ran up against a class full of static methods. Only static methods.  The class implements Serializable.



  • @Thuktun said:

    Trying to clean up the scary Java codebase we enherited, I ran up against a class full of static methods. Only static methods.  The class implements Serializable.


    Well it is serializable, technically. ;o)



  • What does the implementation of serializable look like?

    Is it like .Net in that you can either decorate the class with a SerializableAttribute attribute or implement ISerializable interface to have full control?



  • @C-Octothorpe said:

    What does the implementation of serializable look like?

    Is it like .Net in that you can either decorate the class with a SerializableAttribute attribute or implement ISerializable interface to have full control?

    I've had FxCop urge me to do both at once.



  • That must have been hot.



  • @C-Octothorpe said:

    What does the implementation of serializable look like?

    Is it like .Net in that you can either decorate the class with a SerializableAttribute attribute or implement ISerializable interface to have full control?

     

    In Java you have to implement the Serializable interface for (semi-)automatic serialization or the Externalizable interface for full control.

    This is because Java didn't even have attributes (which they call Annotations) until Java 5.



  • @powerlord said:

    @C-Octothorpe said:

    What does the implementation of serializable look like?

    Is it like .Net in that you can either decorate the class with a SerializableAttribute attribute or implement ISerializable interface to have full control?

     

    In Java you have to implement the Serializable interface for (semi-)automatic serialization or the Externalizable interface for full control.

    This is because Java didn't even have attributes (which they call Annotations) until Java 5.

     

    I see...  So back to my original request, what does this implementation look like?  Unless it's just an empty method, which it probably is...



  • @C-Octothorpe said:

     

    I see...  So back to my original request, what does this implementation look like?  Unless it's just an empty method, which it probably is...

     

     

    The only difference would be that one class has "Implements Serializable" in it's declaration, I think.

     



  • @BC_Programmer said:

    @C-Octothorpe said:

     

    I see...  So back to my original request, what does this implementation look like?  Unless it's just an empty method, which it probably is...

     The only difference would be that one class has "Implements Serializable" in it's declaration, I think.

    Oh man, you made me Google Java docs...  I hate you.  :P

    What I meant was the actual implementation of the interface, other than the class declaration. What does writeObject and readObject look like...



  • @C-Octothorpe said:

    @BC_Programmer said:

    @C-Octothorpe said:

     

    I see...  So back to my original request, what does this implementation look like?  Unless it's just an empty method, which it probably is...

     The only difference would be that one class has "Implements Serializable" in it's declaration, I think.

    Oh man, you made me Google Java docs...  I hate you.  :P

    What I meant was the actual implementation of the interface, other than the class declaration. What does writeObject and readObject look like...

     

    That's the point, though — Java's Serializable interface doesn't actually specify any methods, so just adding "implements Serializable" to the class declaration is enough. See here.


  • ♿ (Parody)

    @Someone You Know said:

    @C-Octothorpe said:
    @BC_Programmer said:
    @C-Octothorpe said:
    I see...  So back to my original request, what does this implementation look like?  Unless it's just an empty method, which it probably is...

    he only difference would be that one class has "Implements Serializable" in it's declaration, I think.

    What I meant was the actual implementation of the interface, other than the class declaration. What does writeObject and readObject look like...

    That's the point, though — Java's Serializable interface doesn't actually specify any methods, so just adding "implements Serializable" to the class declaration is enough. See here.
    Yeah, the only thing you can do, is add a static long serialVersionUID, which is meant to be changed when the details of the class change, so you don't try to de/serialize different versions together.


  • readObject/writeObject don't have to be implemented but they allow you to fine-tune the serialization process. If you're really curious how serialization works, look at the code for ObjectInputStream and ObjectOutputStream.



  • @Thuktun said:

    Trying to clean up the scary Java codebase we enherited, I ran up against a class full of static methods. Only static methods.  The class implements Serializable.
    Exactly how do they get a reference to serialize?



  •  @piskvorr said:

    Well it *is* serializable, technically. ;o)

     

    Indeed - the serialized output is shown below:

     


Log in to reply