This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random question

 
Isaac Hewitt
Ranch Hand
Posts: 191
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing a program where I use a JWindow as sticky notes. I give each note and ID using code like this.



When I run some tests I get some of the ID numbers printed with a minus sign preceding the number. I would like to be able to not have a minus sign there. What should I do. Thanks.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The real question is why are you using a random number generator to assign an ID? What happens if you happen to assign the same ID to two different objects.
[ July 19, 2008: Message edited by: Garrett Rowe ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Garrett. A better approach would be to use a static field:

Note that I made the method synchronized to make sure that the increase is atomic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic