Java static variable for auto-increment (userID) ObjectOutputStream
Solution 1
I am not saying it's a good solution, but the easiest way to solve this is by also persisting the value of your static "nextmember" field to the file.
e.g.
ObjectOutputStream objectOutputStream = ...;
objectOutputStream.writeInt(MyClass.nextmember);
// write your users
ObjectInputStream objectInputStream = ...;
MyClass.nextmember = objectInputStream.readInt()
// read your users
Solution 2
There is really no sense to persist the sequence with the object.
Let's play with this thought, we will save the nextmember
with an object. Imagine the following series:
- Create instance1 of
MyClass
, id=1, seq=1 - Create instance2, id=2, seq=2
- Serialize instance1, id=1, seq=2
- Create instance3, id=3, seq=3
- Deserialize instance1, set seq=2
- Create instance4, id=3, seq=3
So, you got 2 instances, (instance3 and instance4) with the same id.
If you really want to seralize the state, you could have a separate class (and instance) for the sequence. But it still should be a static property of the class.
public class Seq implements Serializable {
private int next = 1;
public synchronized int getNext() {
return next++;
}
}
public class MyClass implements Serializable {
private static Seq seq;
public MyClass() {
userid = seq.getNext();
}
}
You should (de)serialize the sequence separately, and set it to the MyClass with separate getters/setters.
Notice that the sequence number may be accessed from more than 1 threads, so the access to it should be synchronized
.
Erik Haider Forsén
Updated on June 04, 2022Comments
-
Erik Haider Forsén almost 2 years
I have this assignment at school. It includes making a "Person" class, and put each object in a list. Each Person object should have a unique ID. This is achieved by defining a static int in the class, like this:
public class MyClass implements Serializable { private static int nextmember; private int userid; public MyClass() { userid = nextmember++; } }
This works well enough, first object gets userid 1, next object userid 2 etc. My challenge is how to deal with this when also saving to file? I use ObjectOutputStream and ObjectInputStream.
So if I create 3 objects, 1, 2 and 3, close my program (everything is saved to file), re-open the program (all 3 objects are present), and create a fourth object, the fourth object is given userid 1. How can I preserve the nextmember value through closing / re-opening the program?
Tried googling for it, but all I could find on the subject is that "it doesn't make sense to serialize static variables", so maybe I should find a different approach to userid management?
Thanks,
Erik -
Xander about 11 yearsI guess it should be "public synchronized int getNext()" without the static keyword, just saying.
-
Erik Haider Forsén about 11 yearsThanks for the input. I see I'll have to read upon serialization, but for this assignment I think I'll stick to @Xander solution. Definitely something I should look more into though.
-
Erik Haider Forsén about 11 yearsThanks! Helped a lot. I'm not the one to judge how good your solution is (yet), but this solution is at least something that's in line with what we've learned so far.