This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Why can't I access the class variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can Watch "Why can New topic
Author

Why can't I access the class variable?

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hello,

I am a little baffled here.
I wanted to check about multiple thread with the same name so I code the following:





Everything work fine except for the final 'println' (after the for loop).
Gives me a compiler error:

ThreadTesterClass.java:17: cannot find symbol
symbol : variable T
location: class ThreadTesterClass
System.out.println(T.value);
^


I am wondering why it cannot see the object 'T', while inside the for loop it works fine.

Thank you.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3477
    
  13
You declare T inside the for loop, therefore the scope of T is inside the for loop. Your print statement is outside the for loop, so the compiler doesn't know what T is.


Joanne
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Oh..Thanks Neal..
Didn't know that.. for loop is also like a method?
The variables declared inside the loop also have method scope?

Thanks mate.. strange how I missed that...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3477
    
  13
The scope of a variable is from the point it is declared, to the closing } that matches the first unmatched opening { preceeding the declaration. These opening and closing braces can be around method/constructor declarations, for/while loops, if clauses or even just a block of code
[ July 30, 2008: Message edited by: Joanne Neal ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't I access the class variable?