• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot find symbol

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

Some issue I am having with some code I wrote. The code as follow:



I am getting a 'Cannot find symbol' error that says srch can't be found. Why am I getting this error? srch is declared in the constructor already so why this error? Hope someone can advise. Thanks.

regards
John
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post SSCCE.

It is not enough to declare variables in constructor in order to use them throughout your class. You need to declare them as class fields (outside the constructor), otherwise they just act as local variables for the constructor (or any other block of code).

Edit: Playing around with threads without getting along with scope of variables is a tricky business by my oppinion.
 
John Paterson
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kemal Sokolovic ,

Noted. Thanks.

regards
John
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

In java you got different scopes, class level, method level, etc...

So had you declared these variables in class level, you would not have got the error. But here, you have declared them in constructor, so compiler can see them inside that particular constructor only, and not in other scopes....so you are getting "can not find symbol" error....


Hope that helps.
Regards
 
He's giving us the slip! Quick! Grab this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic