File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Cannot Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot "see" a method from a different class?" Watch "Cannot "see" a method from a different class?" New topic

Cannot "see" a method from a different class?

Gary Hill

Joined: Nov 06, 2010
Posts: 15

I am new so may have some terminology wrong!

I am trying to access a method from my main class, i have created an object and i can access this method from Main.

But if i then try and access it from another method it cannot be seen and i cannot compile my code.

In public void test() the compiler says "variable Window cannot be found". Obviously this is not supposed to be a variable!
It also has a thin red squigly line under Window (as its an error in Netbeans)

I have stripped the code down to the minimum for clarity on here.

I would appreciatte some help. Thanks



Please see code below:

Tom Reilly

Joined: Jun 01, 2010
Posts: 618
Your Window variable is local to the main() method. Methods can not see local variables declared in other methods. If you want to see a variable from multiple methods, you must declare it at the class level.
J. Insi
Ranch Hand

Joined: Aug 11, 2010
Posts: 90
yes, he was right.

you need to change the variable declaration to be class level
instead of local level (those located at your main method only).

Here more revised from your code.
Put the variable out of your main method.

Gary Hill

Joined: Nov 06, 2010
Posts: 15
Thanks J Insi and Tom

That has solved my problem!

Its obvious now


I agree. Here's the link:
subject: Cannot "see" a method from a different class?
It's not a secret anymore!