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 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 Murach's Java Servlets and JSP this week in the Servlets 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: 3419
    
  12
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: 3419
    
  12
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?
 
Similar Threads
jtips exam#1, question 58
two objects of the same class can access their private data
Cannot Resolve Symbol Error!
Thread Question
how to call getPriority() on the main thread