• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Cannot "see" a method from a different class?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Regards


Gary



Please see code below:





 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks J Insi and Tom

That has solved my problem!

Its obvious now

Regards


Gary
reply
    Bookmark Topic Watch Topic
  • New Topic