1

I have code that auto increments ids and it works fine. But when I serialize the instance and load it again with existing ID the new ones are counting from 1. Is there a way that if I have ID1 and ID2 existing the new one would be 3 not 1?

String fName;
String lName;
String address;
String phoneNum;
int ID;
static int PAT_ID = 1;
ArrayList<Invoice> p_invoiceList;

public Patient(String fName, String lName, String address, String phoneNum) {
    super(fName, lName, address, phoneNum);
    p_invoiceList = new ArrayList<Invoice>();
    ID = PAT_ID;
    PAT_ID++;

}


public int getId() {
    return ID;
}
Makoto
  • 104,088
  • 27
  • 192
  • 230

1 Answers1

-2

Java serialization ommits static fields. They are not serialized and are ignored during deserialization (see https://stackoverflow.com/a/6429497/1849837)

You have static PAT_ID that after deserialization will be set to 1 by initalizer. Then ID will be set to 1.

Bartosz Bilicki
  • 12,599
  • 13
  • 71
  • 113