Syntax Error
Cannot find symbol: variable 'employee' in method 'int main(java.lang.String[])'
What This Error Means
This error occurs when Java cannot find a declared variable or method within the current scope. This can be caused by a typo in the variable or method name, or by using a variable or method before it is declared.
Why It Happens
This error typically happens when a developer tries to use a variable or method before it has been declared. Java reads the code from top to bottom, so if a variable or method is used before it is declared, Java will not find it and throw this error.
How to Fix It
- 1To fix this error, make sure to declare the variable or method before using it. If you are using a variable, make sure it is declared with the correct data type. If you are using a method, make sure it is declared with the correct parameters and return type.
Example Code Solution
❌ Before (problematic code)
Java
public class Employee {
public static void main(String[] args) {
System.out.println(employee.getAge());
}
}✅ After (fixed code)
Java
public class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return this.age;
}
public static void main(String[] args) {
Employee employee = new Employee("John Doe", 30);
System.out.println(employee.getAge());
}
}Fix for Cannot find symbol: variable 'employee' in method 'int main(java.lang.String[])'
Browse Related Clusters
Related JAVA Errors
Related JAVA Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error