aspose file tools*
The moose likes Java in General and the fly likes Numbering Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Numbering Objects" Watch "Numbering Objects" New topic
Author

Numbering Objects

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I want to assign a sequential number to each instance of a class so that I can reference the order later.
What is the best way to do this?
Can I just use a static variable in the class and then increment it as each object is created in the constructor?
BTW, these objects are going to go into a Vector and then I'm going to pull them out and check their type with getClass().getName() and then I need to know which instance I've got.
Maybe there is a better way I'm not thinking of?
Thanks,
Drew
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Originally posted by Drew Lane:
I want to assign a sequential number to each instance of a class so that I can reference the order later.
What is the best way to do this?
Can I just use a static variable in the class and then increment it as each object is created in the constructor?
BTW, these objects are going to go into a Vector and then I'm going to pull them out and check their type with getClass().getName() and then I need to know which instance I've got.
Maybe there is a better way I'm not thinking of?
Thanks,
Drew


Incrementing a static int and assigning that to an instance it is the most straight forward way of doing it. Sounds like you're on the right track to me.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Pavel Halas
Ranch Hand

Joined: Jul 05, 2003
Posts: 35
You know the easiest way -- static variable. But there could be another factor for using another technique.
What about deleting an object? Will it mean decreasing of sequential number? What about number of next inserting member?
Why not to use Hash* collection insted of vector? Your sequential number chould be the key...
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
If you aren't going to have multiple threads accessing the Vector then I would suggest using an ArrayList since it is faster, or if you decide to use a Hash* class then avoid Hashtable as it is slow like Vectors.
And even if you do need sychronization, you can probably still get around not using Vectors.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i might be misinterpreting it or something but how "static" variable would work here? because the requirement says,

sequential number to each instance of a class

so the var that keeps track of the things should be an instance var rather than a static one right?
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
also
how about using hashCode for each object rather than creating something of our own IF we don't plan to serialize objects and use it across different JVM invocations as the hascode might change across different JVM invocations..
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
ooops
we need sequential numbers. hascode doesn't work
sorry about it..
regards
maulin
Pavel Halas
Ranch Hand

Joined: Jul 05, 2003
Posts: 35
Originally posted by Maulin Vasavada:
... how "static" variable would work here?

Easy.
You would store sequential numbers in some instance variable.
But. What is the actual number (next instance sequential)? Then you'll check the static variable and get the value.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

[ July 13, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
oh, okay...now i c what u ppl meant.
thanks
maulin
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Jim, I have used exactly that technique with count and number on some multi-threaded programs. I put the instance number in any log messages, just so I can tell them apart when their messages are interleaved. Some vendor code I use just puts thread-id in the log messages, but numbers starting with 1 were easier for me to follow.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Numbering Objects