Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Numbering Objects

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pavel Halas
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooops
we need sequential numbers. hascode doesn't work
sorry about it..
regards
maulin
 
Pavel Halas
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ July 13, 2003: Message edited by: Jim Yingst ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, okay...now i c what u ppl meant.
thanks
maulin
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic