Fix Illegal Start of Expression Java Compile Error
Fix the Java illegal start of expression error, usually caused by a missing brace or semicolon above, a nested method or a stray modifier.

"Illegal start of expression" is the compiler telling you it found a token where no expression can begin. It tells you very little about the cause, and the line it points at is usually not the line with the mistake. The real problem is typically a few lines above: an unclosed brace, a missing semicolon, or a declaration written where a statement should be. This post lists the causes in order of how often they occur, each with the code that triggers it and the fix, and ends with habits that prevent the whole family. The compiler is javac from JDK 25.
App.java:9: error: illegal start of expression
public void helper() {
^The caret points at the first token the parser could not accept, which is the symptom. Work backwards from it.
Cause 1: a missing closing brace above
public class App {
public static void main(String[] args) {
System.out.println("start");
// missing closing brace for main
public void helper() { // error: illegal start of expression
System.out.println("help");
}
}The parser is still inside main when it reaches public void helper(). A method cannot be declared inside another method, so it reports the public keyword as illegal. The missing brace is on the line before, or earlier. Fix it by closing main, and the error vanishes. Consistent indentation, which an IDE applies on save, makes this visible: the method that is indented one level too far is the clue. Count braces from the top of the file if the structure is not obvious; most editors highlight the matching brace.
Cause 2: a missing semicolon or parenthesis
int total = price * quantity // missing semicolon
int tax = total / 5; // error is reported hereWithout the semicolon, the parser reads int tax as a continuation of the previous expression and fails on int. Often javac reports "';' expected" for this, and "illegal start of expression" appears when an unmatched parenthesis confuses the parse further down:
if (total > 100 { // missing closing parenthesis
discount = 10;
}Look at the end of the line above and at the previous few lines for an unbalanced (.
Cause 3: a modifier on a local variable
void count() {
public int total = 0; // error: illegal start of expression
total++;
}public, private, protected and static apply to fields, methods and classes, not to variables declared inside a method. A local variable has no visibility, so the parser rejects the modifier. Remove it. A variable that must be shared across methods belongs as a field, declared in the class body outside any method. final is allowed on locals, which makes the rule easy to forget.
Cause 4: a statement where only declarations may go
public class App {
System.out.println("loading"); // error: <identifier> expected, or illegal start of expression
int count = 0;
}The body of a class can contain fields, methods, constructors and initialiser blocks, but not free-standing statements. Put the statement inside a method, a constructor or an initialiser block:
public class App {
int count = 0;
App() {
System.out.println("loading");
}
}The same message appears when a closing brace closed the class early: everything after it is outside the class, and the first method after it is reported.
Cause 5: a stray or mismatched token
int[] values = {1, 2, 3};
for (int v : values) {
total += v;
}} // extra brace closes the method early
public void next() { } // class, interface, enum, or record expectedAn extra closing brace, a stray comma, a smart quote pasted from a document, or an operator with no operand produces errors that begin at the stray token. Pasted code is a frequent source: curly quotes and non-breaking spaces look fine and are not Java. A lone = in a condition, an arrow written as a different character, or a lambda without parentheses where they are required can also trigger it. If the line looks right, retype it by hand. Switch expressions and text blocks bring their own syntax: a case arrow without the right form inside a switch statement or a text block with its closing delimiter on the same line as its opening, both give errors that start with this message.
Prevent it
Use an IDE or an editor with a language server, so the red underline appears as you type and points at the real position. Format on save so the indentation shows structure. Compile often, and in small steps; an error among fifty new lines is hard to find, and one among five is obvious. Match braces as you type them: write the closing brace when you write the opening one, then fill the inside. When the compiler gives a list of errors, fix the first and recompile, because the later ones are often consequences of the first, as noted for cannot find symbol.
Reading the compiler output properly
javac prints the file, the line, the message, the source line and a caret. It also prints a count at the end, such as 1 error. When there is more than one error, they are ordered by position, and the first is the best lead. Use -Xmaxerrs if the list is truncated. Add -Xdiags:verbose for fuller explanations of some messages, which helps with generics but rarely with parse errors.
A reliable technique for a stubborn case is bisection. Comment out the second half of the file, compile, and see whether the error disappears. If it does, the problem is in the half you removed, so restore that and halve it again. Within a few steps you reach the line. Build tools show the same output, and an IDE shows it inline.
Cases from newer language features
Recent Java versions add syntax that older code did not meet. A text block that opens with three quotes must start its content on the next line. A switch expression must end each arrow branch with an expression or a block, and a yield is needed to return from a block. Record declarations put the components in parentheses after the name, and a record cannot be declared with a body that looks like a class's fields. A mistake in any of these sometimes surfaces as an illegal start message at the first unexpected token. If you are on an older JDK than the code expects, the compiler may reject the new syntax with exactly this message, so check the version with java -version and javac -version before hunting for a typo.
Where this does not apply
The message is specific to parse errors. If the code parses and a name is wrong, the message is different, and the related errors are in incompatible types. Building a first program and reading these messages is part of getting started with Java. The grammar that the parser follows is defined in chapter 14 of the Java Language Specification, and the options of the compiler in the [javac manual](https://docs.oracle.com/en/java/javase/25/docs/specs/man/javac.html).


