Error Message Name:
Error: cannot find symbol
Symbol: variable {variable name}
Example:
public String alpha = "ABC";
public String getOutput()
{ String output = "The output is ";
return this.output + this.alpha;
}
The above code would yield this error message:
Error: cannot find symbol symbol: variable output
Cause: You are using the key word this to call a local variable. Remember, you can only use this to refer to methods, constructors or instance variables of the current class. You may not use this to call any local variables, or methods and constructors of other classes. Since Java cannot find an instance variable named output, an error is displayed.
Solution: Remove this when calling a local variable:
public String alpha = "ABC";
public String getOutput()
{ String output = "The output is ";
return output + this.alpha;
}
Description: Method is not changing the value of the instance variable correctly.
Example:
private int num = 10;
public int specialOperation(int num)
{ for (int i = 0; i < num; i++)
{ num ++;
}
return num * num;
}
Suppose we call specialOperation(5) in the main method. The method should return (10+5)*(10+5) = 225. But, it is returning 100.
Cause: When we write num, we are actually referring to the parameter num. Hence, the method is returning (5+5)*(5+5) = 100. To refer to the instance variable num, we must use the keyword this.
Solution: Here is how we would fix the sample code to return 225:
private int num = 10;
public int specialOperation(int num)
{ for (int i = 0; i < num; i++)
{ this.num ++;
}
return this.num * this.num;
}