• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot resolve Varibles

 
Bob Dundy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Point
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic