Syntax Error
Cannot find symbol: variable 'name' in void main(String[] args)
What This Error Means
This error occurs when the Java compiler is unable to find a declared variable or method. In this case, the variable 'name' is not declared within the scope of the main method.
Why It Happens
This error typically happens when a developer tries to use a variable or method that has not been declared or initialized before its usage. In this specific case, the variable 'name' is being used in the main method without being declared or initialized.
How to Fix It
- 1To fix this error, the developer should declare and initialize the 'name' variable before using it. Here's the corrected code:
- 2// Before (broken code)
- 3public class HelloWorld {
- 4 public static void main(String[] args) {
- 5 System.out.println(name);
- 6 }
- 7}
- 8// After (fixed code)
- 9public class HelloWorld {
- 10 public static void main(String[] args) {
- 11 String name = 'John Doe';
- 12 System.out.println(name);
- 13 }
- 14}
Example Code Solution
❌ Before (problematic code)
Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(name);
}
}✅ After (fixed code)
Java
public class HelloWorld {
public static void main(String[] args) {
String name = 'John Doe';
System.out.println(name);
}
}Fix for Cannot find symbol: variable 'name' in void main(String[] args)
Browse Related Clusters
Related JAVA Errors
Related JAVA Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error