aspose file tools*
The moose likes Threads and Synchronization and the fly likes Why are private members accessible to threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why are private members accessible to threads?" Watch "Why are private members accessible to threads?" New topic
Author

Why are private members accessible to threads?

Andre Bell
Greenhorn

Joined: Jul 14, 2005
Posts: 2
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?

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Hi,

Welcome to JavaRanch!

All the code actually contained inside a class definition is, for the purpose of access control, considered part of that class. This includes all inner classes. Doesn't matter what thread it's invoked from: the code in the anonymous class has access to the members of MyClass.


[Jess in Action][AskingGoodQuestions]
Andre Bell
Greenhorn

Joined: Jul 14, 2005
Posts: 2
Right. So for all intents and purposes, this is really an inner class. Once you know it's an inner class, then scope and visibility is easy to figure out. Thanks. Mughall and Rasmussen call this a (inner-class) local class which, have access to their outer instance (enclosing context).
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why are private members accessible to threads?