How can I serialize static variables

Are there anyways to get around the process and serial static variables? I have a Quiz class which contains a static variable that holds the next availiable ID number for a quiz object, but upon reading the object back this number goes back to the default of 0.
[268 byte] By [ErfangCa] at [2007-10-2 12:52:36]
# 1

If you think abou it, it doesn't make sense to serialize static varialbes. They're part of the state of the class, not the object. When you look at one of your objects, the "next available ID" is not part of that object.

IMHO, your best bet is to explicitly serialize that value at the time you serialize the objects, and deserialize it at the other end.

jverda at 2007-7-13 10:05:52 > top of Java-index,Core,Core APIs...
# 2

It's going to be dangerous to serialize the static value along with individual instances... e.g

1. create 4 quiz objects (1,2,3,4) and serialize them, all storing next available id as 5

2. create 3 more quiz objects (5,6,7) so the next available id is 8

3. deserialize one of the initial objects and the next available id gets reset to 5

4. create new quiz object, id gets set to 5, even though another instance of quiz exists with that id -- Ooops

Have included code here for how to do it just in case you can ensure that the above scenario cannot arise or be a problem

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

public class ForumQuestion {

public static void main( String[] args ) throws IOException, ClassNotFoundException {

ByteArrayOutputStream os = new ByteArrayOutputStream();

ObjectOutputStream out = new ObjectOutputStream( os );

Quiz q = null;

for( int i = 0; i < 4; i++ ) {

q = new Quiz();

}

out.writeObject( q );

out.close();

for( int i = 0; i < 3; i++ ) {

new Quiz();

}

ByteArrayInputStream is = new ByteArrayInputStream( os.toByteArray() );

ObjectInputStream in = new ObjectInputStream( is );

in.readObject();

new Quiz();

}

}

class Quiz implements Serializable {

private static volatile int nextId = 1;

private int myId;

public Quiz() {

myId = nextId++;

System.out.printf( "Created quiz %d%n", myId );

}

private void writeObject( ObjectOutputStream os ) throws IOException {

os.defaultWriteObject();

os.writeInt( nextId );

System.out.printf( "Stored quiz %d, nextId %d%n", myId, nextId );

}

private void readObject( ObjectInputStream is ) throws IOException, ClassNotFoundException {

is.defaultReadObject();

nextId = is.readInt();

System.out.printf( "Restored quiz %d, nextId %d%n", myId, nextId );

}

}

The Quiz class shows how to serialize the static field along with the instance, but the output illustrates the danger:

Created quiz 1

Created quiz 2

Created quiz 3

Created quiz 4

Stored quiz 4, nextId 5

Created quiz 5

Created quiz 6

Created quiz 7

Restored quiz 4, nextId 5

Created quiz 5

sorabaina at 2007-7-13 10:05:52 > top of Java-index,Core,Core APIs...
# 3
Thanks guys.
ErfangCa at 2007-7-13 10:05:52 > top of Java-index,Core,Core APIs...