How do you fix "error: not a statement" in Java?

← Java answers

A line contains an expression that does nothing on its own, like x + 1; or a == b;. Assign the result, call a method, or correct the stray or misspelled token.

Only certain expressions can stand alone as a statement: assignments, increments, method calls and object creation. Anything else is rejected.

int x = 5;
 
x + 1;            // error: not a statement, the result is discarded
x = x + 1;        // fine: assignment
 
x == 5;           // error: a comparison on its own does nothing
if (x == 5) { }   // fine: used in a condition
 
"hello";          // error: a bare value
System.out.println("hello");   // fine: a call
 
list.size;        // error: size is a method
list.size();      // fine
  • Check the line above too: a missing semicolon or brace can make the next line look wrong.
  • Misspelled keywords (Int, retrun) are read as identifiers and also give this error.
  • Using = where you meant a method call, or . where you meant ;, are common typos.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required