3

I need to persist several classes with A LOT of static fields (which are arrays filled & modified during the runtime). It would take a lot of effort to convert from static fields to instance vars, so I'd rather go for a quick solution, if any.

So far the path of least resistance is to cook my own writeObject() for each class.

Alternatively, i dont need Serialization as such - any mechanism to store/load an object from persistent storage will do. E.g. XMLDecoder to decompose the bean objects, etc.

4
  • 5
    But the static fields are, well, static. What would you want to happen if two instances which were serialized when the static fields had different values were subsequently deserialized? What values would be in the static fields at that point? Commented Mar 16, 2011 at 18:21
  • 2
    db4o can store static variables, if configured to do so. But you better think about your design here. Commented Mar 16, 2011 at 18:23
  • kprevas asks a good question. Any solution that involves serializing static members that are changed at runtime is going to run into that issue. The most basic, but still good, solution will probably involve making the members not static. Commented Mar 16, 2011 at 18:24
  • Please don't use mutable statics (and that includes singletons). Commented Mar 16, 2011 at 22:01

3 Answers 3

3

You could write a method to use reflection to serialize all static methods. A static method can be marked transient which would normally be redundant, but you could use this to highlight static fields you don't want to serialize. You can call this method from your writeObject() to save you having to do this for every field.

Sign up to request clarification or add additional context in comments.

1 Comment

What a great answer :) I forgot all about Reflection
1

I'd create a new class that gathers all those static fields, lets call it StaticInfoClass. In StaticInfoClass create all the fields non-static. Create a property in the old class that is type of StaticInfoClass. All instances of the your original class will hold a reference to a unique instance of StaticInfoClass, that will contain all the arrays filled and modified during the runtime. If you have threads make sure you make it a bean with synchronized methods to avoid race conditions.

With this approach, in essence, you are implementing static fields without actually using the static modifier. This simple approach should easy a lot your serialization and deserialization problems and most frameworks will handle it very easily.

Also remember that static fields and global state in programs are normally a bad practice, it should be avoided. I tend to not use static if it is not to declare constants or other trivial data structures that definitely do not changed in runtime.

Comments

0

Have not tried this but might work. if your class defines an array of ObjectStreamField objects named serialPersistentFields , then you can explicitly declare the specific fields saved. You can shove any value into it, even the static fields.

private static String lastUser;
private static int maxAge;
private final static ObjectStreamField[]
    serialPersistentFields = {
      new ObjectStreamField(
      "lastUser", String.class),
      new ObjectStreamField("maxAge", int.class)
    };

Then you will have to implement readObject, to fetch these values and set it for your static variables. So basically this mechanism allows you to marshall/unmarshall fields from anywhere via serialization. You just need to know what to do with them once you read them back. I would still recommend paying the price for moving your statics to member variables.

details : http://java.sun.com/developer/technicalArticles/ALT/serialization/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.