This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Random question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random question" Watch "Random question" New topic

Random question

Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 191

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

Joined: Jan 17, 2006
Posts: 1296

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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rob Spoor

Joined: Oct 27, 2005
Posts: 20077

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.

How To Ask Questions How To Answer Questions
Have you checked out Aspose?
subject: Random question
jQuery in Action, 3rd edition