Results 1 to 7 of 7
  1. #1
    powerpravin is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Exclamation How to access all the datafields of a class?

    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 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.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you don't need to do that, just serialize the object and write the entire object to a file. when you need the data, load the object and access its fields as you would normally.

    http://java.sun.com/developer/techni...serialization/

  3. #3
    powerpravin is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    you don't need to do that, just serialize the object and write the entire object to a file. when you need the data, load the object and access its fields as you would normally.

    Discover the secrets of the Java Serialization API
    Thanks for the quick reply!

    As the link explains, I can backup the objects through serialization if I explicitly pass the name of the object.
    But here I am more interested in backing up the data of fields of the class without needing to refer any name. So that in future, if I need to add or remove array lists or change the name of class, I need not to change the code of the method 'backItUp()'

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    My gues would be to write all the data as a string and then write the string as a txt file.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by powerpravin View Post
    Thanks for the quick reply!

    As the link explains, I can backup the objects through serialization if I explicitly pass the name of the object.
    But here I am more interested in backing up the data of fields of the class without needing to refer any name. So that in future, if I need to add or remove array lists or change the name of class, I need not to change the code of the method 'backItUp()'
    I don't get it..... What do you mean when you say not needing to refer to any name?

  6. #6
    powerpravin is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    I don't get it..... What do you mean when you say not needing to refer to any name?
    NIC is the class in which all the arraylists and the method backItUp is present. I want to write backItUp() in such a way that it will automatically search for all the fields the class NIC contains.(In the example I have given above, after searching it should have references for all the array lists.) After that the method should traverse all the array lists and then save the contents into a file.

    In simple words, I want create a method which will take back up of all the fields of class in which the method resides.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jackycui View Post
    You can create a Class called AllData as below.
    [snip ...]
    Oh please; that class has just three set and get methods for three collections. How is that going to solve the problem? Or do you just want to show your url again?

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Package access for class
    By jomypgeorge in forum New To Java
    Replies: 22
    Last Post: 12-01-2010, 06:46 AM
  2. Access an array from a different class
    By chief100 in forum New To Java
    Replies: 5
    Last Post: 11-02-2010, 07:57 AM
  3. why i cant access a method in a sub class?
    By javanew in forum New To Java
    Replies: 7
    Last Post: 05-03-2010, 06:29 PM
  4. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  5. Access LinkedList from another class
    By jboy in forum New To Java
    Replies: 20
    Last Post: 09-12-2009, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •