JAVAWarningApril 15, 2026

Syntax Error

Cannot find symbol: variable 'age' in main method

What This Error Means

This error occurs when the Java compiler cannot find a declared variable, method, or class, but it is actually defined. The error message indicates the name of the missing item.

Why It Happens

This error usually happens when the variable is declared inside a block of code that has been closed. For example, if you declare a variable inside an if statement, the compiler will not be able to find it outside that block.

How to Fix It

  1. 1To fix this error, you need to declare the variable outside the block of code where it is being used. In the example below, the variable 'age' is declared inside the if statement, so we need to move it outside.

Example Code Solution

❌ Before (problematic code)
Java
class Test {
public static void main(String[] args) {
if (true) {
int age = 25;
System.out.println(age);
}
}
}
✅ After (fixed code)
Java
class Test {
public static void main(String[] args) {
int age = 25;
if (true) {
System.out.println(age);
}
}
}

Fix for Cannot find symbol: variable 'age' in main method

Related JAVA Errors

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error