JB Header
Java - Compiler Error "Error: not a statement"
If you are getting the following error(s) at compilation time -
Error: not a statement
which may be accompanied by -
Error: ';' expected 
Error: ')' expected
Then there are two possible reasons for these compiler errors to happen -
Possible Reason 1: Applicable in case of Java 8 lambda expressions - When trying to assign a Java 8 Lambda ExpressionRead Lambda Expressions Tutorial to a Functional Interface Click to Read Detailed Article on Functional Interfaces instance like this -
import java.util.function.Function;
public class ErrorExample{
  public static void main(String args[]){
    Function func= Integer i -> i.toString();

The lambda assignment statement above will give the compilation errors - Error: not a statement along with Error: ';' expected
Solution: Enclose the statement Integer i in parenthesis/circular brackets. This is because when specifying the type of an argument in lambda expressions it is mandatory to add parenthesis around the arguments. The correct assignment statement without the compilation errors would then be -
Function func= (Integer i) -> i.toString();

Possible Reason 2: Applicable to Java Code in General - Compiler expected a statement in that line but instead got something different. Lets see couple of examples to understand the scenarios in which this error could occur -
Example 1:
Incorrect: if (i == 1) "one";
The above statement will give a "Error: not a statement" compilation error because its actually not a proper statement. The corrected statement is -
Corrected: if(i==1) System.out.println("one");

Example 2
Incorrect: System.out.println("The "+"value of ";+i+" is "+i+" and j is"+j);//i.j being integers
The above statement will give "Error: not a statement" along with "Error: ')' expected" compilation errors because of the incorrectly added extra semicolon(;) after "value of " in the above statement. If we remove this extra semicolon then the compiler errors are removed.
Corrected: System.out.println("The "+"value of "+i+" is "+i+" and j is"+j);