aspose file tools*
The moose likes Beginning Java and the fly likes Cannot resolve Varibles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot resolve Varibles" Watch "Cannot resolve Varibles" New topic
Author

Cannot resolve Varibles

Bob Dundy
Greenhorn

Joined: Apr 27, 2004
Posts: 2
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

Joined: Dec 10, 2001
Posts: 7023
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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().


[Jess in Action][AskingGoodQuestions]
Bob Dundy
Greenhorn

Joined: Apr 27, 2004
Posts: 2
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Dec 06, 2001
Posts: 3061
Point
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot resolve Varibles