How to serialize static data members of a Java class?

后端 未结 9 1860
孤城傲影
孤城傲影 2020-11-27 17:30

When we serialize objects, static members are not serialized, but if we need to do so, is there any way out?

相关标签:
9条回答
  • 2020-11-27 17:53

    You can control serialization by implementing:

    private void writeObject(ObjectOutputStream out) throws IOException;
    
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
    

    There's a full description of serialization http://java.sun.com/developer/technicalArticles/Programming/serialization/.

    As other answers have said, it doesn't really make sense to serialize statics as it's the object not the class you're serializing and needing to do so smells like you've got other issues with your code to me.

    0 讨论(0)
  • 2020-11-27 17:53

    Static members belong to the class, not to the individual objects.

    You should reconsider your data structure.

    0 讨论(0)
  • 2020-11-27 17:55

    To have compact implementation, implement readObject & writeObject in your class call defaultReadObject & defaultWriteObject methods within those methods which handles normal serialization and then proceed with serializing & de-serializing whatever additional fields you need.

    Regards, GK

    0 讨论(0)
  • 2020-11-27 18:06

    Good answers and comments--don't do it. But how?

    Chances are you would be best off creating an object to hold all your "Statics". That object should probably have any static methods from your class as well.

    Every instance of your class can hold this other class--or if you really have to you can make it a singleton that any member can access.

    After you do this refactor, you will find that it should have been done this way all along. You may even find that some of your previous design constraints that were bothering you at a subconsicionce level have vanished.

    You'll probably find that this solution also solves other Serialization problems you hadn't even noticed yet.

    0 讨论(0)
  • 2020-11-27 18:11

    Folks, static doesn't mean IMMUTABLE. For instance, I may want to serialize the whole state of the computation (yes, including static fields -- counters, etc) to resume later, after JVM and/or host computer restarted.

    The right answer to that, as already said, is to use Externalizable, not Serializable, interface. Then you have a complete control on what and how you externalize.

    0 讨论(0)
  • 2020-11-27 18:11

    This is serialization for the static field: newBookingNumber.

    class Booking implements Serializable
    {
    
        /**
         * Generated serial version ID.
         */
    
        private static final long serialVersionUID = 5316748056989930874L;
    
        // To hold new booking number.
        private static int newBookingNumber = 0;
    
        // The booking number.
        private int bookingNumber;
    
    
        /* 
         * Default serializable fields of a class are defined to be 
         * the non-transient and non-static fields. So, we have to 
         * write and read the static field separately.
         */
        private void writeObject(ObjectOutputStream oos)
            throws IOException 
        {
            oos.defaultWriteObject();
            oos.writeObject(new Integer(newBookingNumber));
        }
    
        private void readObject(ObjectInputStream ois)
        throws ClassNotFoundException, IOException 
        {
            ois.defaultReadObject();
            newBookingNumber = (Integer)ois.readObject();
        }
    }
    
    0 讨论(0)
提交回复
热议问题