File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Thread --> run() method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Thread --> run() method" Watch "Thread --> run() method" New topic
Author

Thread --> run() method

Subhro Chowdhury
Greenhorn

Joined: Mar 22, 2007
Posts: 10
A static variable can't be accessed from a non-static method. But we can access a static variable inside run() method.


This code compile and run successfully. How come we are accessing the threadcounter inside run() ?


-Subhro Chowdhury
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Originally posted by Subhro Chowdhury:
A static variable can't be accessed from a non-static method.


Yes it can. Non-static variables can't be accessed from a static method.


Joanne
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

This is easy to understand. A static member (method or variable) doesn't belong to any instance of a class, it belongs to the class itself. Therefore, you can't access, for example, a non-static member from inside a static method, because there's no direct relationship that tells from which instance of the class the non-static member refers to. However, the other way around is perfectly acceptable, because a non-static method knows the class it belongs to, and then can access static members of this class.


Leandro Melo <br />SCJP 1.4, SCWCD 1.4<br /><a href="http://www.pazbrasil.org/" target="_blank" rel="nofollow">http://www.pazbrasil.org/</a>
Subhro Chowdhury
Greenhorn

Joined: Mar 22, 2007
Posts: 10
Got it. Thanks for the explanation.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Thread --> run() method
 
Similar Threads
Thread and locks
Thread synchronization question
Thread lock doubt
synchronized run method.
Another Thread Question