Question:
Quick and easy Java question? Cannot find symbol error?
Eirika Renais
2011-10-01 21:17:41 UTC
Okay, so basically, I have this variable declared in one method and I wish to use that variable in multiple sub-methods, but it says it cannot find the symbol?
Here is an extremely simplified version of my problem.

public class asdf
{
public static void main(String[] args)
{
int a;
testing();
}
public static void testing()
{
a = 4;
System.out.println(a);
}
}


If I try to compile it, I get the following errors:
asdf.java:10: error: cannot find symbol
a = 4;
^
symbol: variable a
location: class asdf
asdf.java:11: error: cannot find symbol
System.out.println(a);
^
symbol: variable a
location: class asdf
2 errors


How should I go about fixing such a problem? (Btw, I've only been learning Java for ~1.5 months, go easy on me)
Four answers:
Vaibhav
2011-10-02 07:00:19 UTC
You need to declare it as a global variable

That should be

class asdf

{

static int a;

add your remaining code here
husoski
2011-10-01 21:26:26 UTC
If you want to share a variable between different static methods, you need to either (a) make it a static member of the class ("static int a;" inside the class, but not inside any method), or (b) pass it as an argument from the function that defines the variable to each static method that uses it.



The subtle part of (b) is that you can't change the value of a primitive variable via an argument, so you need to declare a as Integer (the wrapper class for the primitive int type) in both the variable and the argument types. So main() might look like:



public static void main(String args[])

{

Integer a = 1;

testing(a);

System.out.prinln("After testing(), a=" + a);

}



static void testing(Integer a)

{

a = 4;

...

}
Ian Nightingale
2011-10-01 21:21:30 UTC
You need to declare "a" as a global variable if you want to use it more than one function:



public class asdf

{

int a;

public static void main(String[] args)

{
?
2016-12-03 01:56:42 UTC
In all your strategies aside from important, you're doing some thing like "trans=a million", yet you have no longer declared the variable "trans" in that technique. I only quickly skimmed, yet if you consider which you're only making an project (with out an preliminary statement) to trans on the beginning up of all the different strategies, so only bypass lower back and upload replace something this is like this: trans=a million; with this: int trans=a million; i'm additionally seeing this comparable concern on your code with different variables like transportation, drivedistance, airface, and so on. This has to do with some thing referred to as variable scope. once you declare a variable in one technique, it is not obtainable in yet another technique till you bypass it as between the parameters, or use them as international variables (declared outdoors of any technique, yet this is many times frowned upon). So, only bypass lower back and declare those variables on the beginning up of the technique that they are certainly getting used in. in case you have the different questions, you are able to edit your publish and that i will examine lower back a sprint later. in any different case, attempt to locate the area on "variable scope" on your e book. it could make issues a sprint extra straightforward to appreciate.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...