Originally posted by Andre Bell:
The following code creates an annonymous Thread object, and an annonymous Runnable object, both of which go out-of-scope once main() has ended.
I fail to understand why the "external" thread object (or should I say the thread-of-execution) is able to access the private(!) member count of the Myclass class?
Myclass inherits from Object, and is itself running in a thread-of-execution. Is there another kind of visibility/scope that I should be aware of?
(snip...)
Even though EFH answered your question, I hope you don't mind me throwing in a few extra tidbits.
I'm not entirely sure about the exact details according to the JLS, but I don't think that it is correct to say that an object "goes out of scope" since objects themselves don't have scope. Reference variables on the other hand DO have scope. In this case, the anonymous Thread object that you create presumable has a reference to the anonymous Runnable object that is created. This means that the Runnable object is not available for garbage collection and there is a reference to it that probably has instance scope (is that the correct terminology?) inside a Thread object. This means that even though the temporary anonymous reference to the Runnable object that exists in the byte-code for main() goes out of scope, the object still exists on the heap because the Thread object has a reference to it.
Andre:
Myclass inherits from Object, and is itself running in a thread-of-execution....
What does "itself" refer to? The way I read this, it is referring to "my class". However, it isn't correct to say that your class is running because classes (or objects) aren't executed. Methods are executed. In fact, two methods on the same object (or even the same method on the same object) can be executed by two different threads. So saying that a class (or even an object) is running in a thread of execution is incorrect since the methods of an object can (and often do) run on multiple threads.
Anyway, this is probably all beside the point and some of it is complete suposition, so take it worth the two cents that it is. In the end, you already have the answer to your question, so keep coding!
Layne
p.s. If any of my half-assed guesses are off base, please someone let me know. Threads are definitely NOT my strong suit, and I would like to learn more about them.
[ July 14, 2005: Message edited by: Layne Lund ]