• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Cannot resolve Varibles

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have followed the instructions in creating this quadratic program, but I keep getting these compiler errors:
Error: cannot resolve symbol
symbol : variable A
location: class Quadratic
Error: cannot resolve symbol
symbol : variable B
location: class Quadratic
Error: cannot resolve symbol
symbol : variable C
location: class Quadratic
here is the code:

The errors says line 50, which is the g.drawString( "Solving " +A .....
Please help.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Bob!
The variable A declared in the main method is not available in another method. In other words, variables local to a method are not accessible from another method.
Perhaps you want A to be an instance variable - declared outside of any method.
Getting any ideas?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob,
Welcome to JavaRanch!
The variables A, B, and C are defined inside the method main(). That means, by default, that they're only available inside main(). If you try to use them in paint(), as you do here, the compiler will complain that it doesn't know which A, B, and C you mean, since there are none that apply during paint().
There are a couple of ways to exchange information between methods. One way is by passing the variables as arguments -- just as you pass the string firstNumber to the Integer.parseInt() method, you could imaging passing A, B, and C to paint().
Unfortunately, in this case, paint() doesn't accept any int arguments, so that isn't going to work here. What you could do instead is make A, B, and C into static variables defined in the scope of the class itself, not inside main(). Such variables would be useable by all the methods in the class.

And remove the definitions inside main().
 
Bob Dundy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, now I understand my mistake
I have to define the local variable so that it is not only in one method.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's not in a method, then it is no longer local. Such a variable is called an "instance" variable because each instance of the class gets its own copy of the variable.
Layne
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Layne Lund:
Such a variable is called an "instance" variable...


Unless they're static, as they'll need to be here, in which case they're called "static variables" or "class variables."
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Point
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic