File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Get variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get variable" Watch "Get variable" New topic

Get variable

Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48

[ August 07, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

You can't, not as the code is written. If "number" were an instance variable instead of a local variable, then it would be accessible:

There's potentially another problem: the variable "number" is being accessed by two different threads, and that can lead to problems with the two threads not seeing the same value for the variable. I believe that in this case, it's OK because of the call to join() and the synchronization that it involves. Anyone disagree?

[Jess in Action][AskingGoodQuestions]
Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
your solution works perfectly for what i wanted !

the join() method is the key here, otherwise, i got a 0 (because it reads the variable before the run() completes)

thanks !
I agree. Here's the link:
subject: Get variable
Similar Threads
difference between a class variable and local method variable with an example
Variable Arguments
Doubt with static initializers
Passing value from one method to the next
Retrieving a value ..