JAVAWarningRuntime ErrorMay 31, 2026

Runtime Error

Cannot find symbol. Symbol: method 'toString()' variable 'student' is defined in class 'Main' but never used on line 12

What This Error Means

This error occurs when the Java compiler is unable to find a method, class, or variable that is being referenced in the code. In this case, the error is caused by attempting to call the 'toString()' method on an object without properly casting it.

Why It Happens

This error happens because the developer is trying to call a method on an object that is not of the expected type, or is not properly initialized. In this case, the variable 'student' is declared as an object of a custom class, but the compiler is unable to find the 'toString()' method because it is not being called on a String object.

How to Fix It

  1. 1To fix this error, the developer needs to properly cast the 'student' object to a String object before calling the 'toString()' method. Alternatively, they can override the 'toString()' method in their custom class to return a string representation of the object.

Example Code Solution

❌ Before (problematic code)
Java
public class Main {
    public static void main(String[] args) {
        MyClass student = new MyClass();
        System.out.println(student.toString());
    }
}
✅ After (fixed code)
Java
public class Main {
    public static void main(String[] args) {
        MyClass student = new MyClass();
        System.out.println(student.toString());
        // or 
        System.out.println(student);
    }
}
// Alternative solution in the MyClass class
public class MyClass {
    @Override
    public String toString() {
        return "My Class";
    }
}

Fix for Cannot find symbol. Symbol: method 'toString()' variable 'student' is defined in class 'Main' but never used on line 12

Related JAVA Errors

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error