I have some classes like Client, Employee, Property
I am maintaining the list of all the objects as array lists in a class names NIC.
So Java code for class NIC is like:
class NIC
{
static ArrayList<Employee> employeeList = new ArrayList<Employee>();
static ArrayList<Property> propertyList = new ArrayList<Property>();
static ArrayList<Client> clientList = new ArrayList<Client>();
//rest of the code
public static void backItUp()
{
//this method reads all the objects data from array lists and stores it in a file
}
}
I am stuck on the method backItUp() which is supposed to read all the objects data from array list and store it in a file.
I don’t know if there is any method which can access all the data fields of current class or at least returns a reference of each data field.
Please help. Thanks in advance.
Your best option is to remove the
staticmodifiers from all your fields and methods, and makeNICa proper object that can be instantiated. When you adhere to OOP (Object Oriented Principles), you’ll find everything becomes easier. After that, all you need to do is makeNICimplementSerializable. The default serialization routine will automatically save everything in your class that is not marked astransient.ArrayLists and all other JDK collections are already serializable, so no further work is required. Just read the guide that KDM posted.However, if you decide to add more fields or methods later, you will still end up with compatibility problems since old persisted objects will no longer match the signature of the new, updated class. This is a consideration all programmers must make. Using
serialVersionUIDcan help a little, for instance, in cases where you’re just adding new methods but no new fields, but proper testing is needed to ensure backwards compatibility.