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

    Default Counting/tracking instances of an Object problem

    I have an object which is rather complicated so i'm just going to make a simplified example of how this object keeps count of its instances:

    Java Code:
    class myObj {
    
        static int numOfObj;
        int objId;
    
        //constructor
        myObj() {
            objId = ++numOfObj;
        }
    
    }

    The objId representing the ID should always increment, and this works fine until I (need to) delete some objects later on.

    So this is the path through my program which creates the problem:

    1. Open Program
    2. Autoload > 0 files found (nothing loaded)
    3. Create 5 instances of myObject (IDs are 1,2,3,4,5)
    4. Serialize and save to output files
    5. Finished Processing -> Delete all files to start again
    6. Create a few more instances (IDs are 6,7) //this is desired
    7. Serialize and save to output files
    8. Close Program (but not finished, so files not processed)
    9. Open Program
    10. Autoload > 2 files found (IDs are 6,7) //also desired
    11. Create 1 instance of myObject (ID is 3) //PROBLEM

    the ID in the above example becomes 3 because the program recognizes that the number of Objects is now 2 + new one = 3. But what I really want is a number that always increments and is unique.

    after creating more instances the original files which were loaded (with ID 6,7 in the above example) will eventually be overwritten.

    any suggestions on how I can fix this? really i'm looking for something which will work without having to save the number of orders to a file so that my program relies on a text file to start up correctly.
    Last edited by ozzyman; 04-03-2011 at 01:13 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why must the numbers be monotonically increasing ints? Why not a random long, or two longs as the chances of overlap are low and can be prevented by holding the longs in a HashSet and only using new longs that are not already members of the set?

    Edit 1: Note that I'm just musing. Let's wait to hear what some of the pros in here say.

    Edit 2: Interesting quotes in this thread at StackOverFlow: serialize-static-attributes-in-java
    Last edited by Fubarable; 04-03-2011 at 01:46 AM.

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

    Default

    hi fubarable, thanks. i prefer the ints to make things look simple, but i'm not religiously keen on it so i will change it if necessary. i don't really know about hashsets but if you say it can prevent duplicate numbers then its definately worth me looking into it.

    but then i was just thinking, should i use a method to search the objects IDs and take the highest one and then work from there, or is that incredibly inefficient?

    also, the simple solution would be to reset the 'numOfObj' field to 0 after the files are processed, but then I would lose the all-time record of number of objects.


    EDIT:
    i thought about this and actually I think its right the set the field to 0 once the objects are deleted because the true number is 0. then for my alltime record i could save the total number of files processed to a text file - and my program wont rely on it because its just for the record, not for the pointer/ID number. so i'm going to go ahead and do that, but i'm leaving this thread open in case someone has something more wonderful to share :). Thanks for your time.
    Last edited by ozzyman; 04-03-2011 at 02:12 AM.

Similar Threads

  1. Problem with class instances
    By sdwinder in forum New To Java
    Replies: 7
    Last Post: 10-21-2009, 02:25 AM
  2. Build Instances problem
    By leapinlizard in forum New To Java
    Replies: 4
    Last Post: 04-21-2009, 11:17 AM
  3. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 03:10 PM
  4. Object Reflection: Creating new instances
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:13 PM
  5. Naming object instances
    By oldgit in forum New To Java
    Replies: 9
    Last Post: 02-08-2008, 12:18 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
  •