How to fix "illegal start of expression" error in Java? Example (original) (raw)
The "illegal start of expression" error is a compile-time error when the compiler finds an inappropriate statement in the code. The java compiler, javac, compile your source code from top to bottom, left to right and when it sees something inappropriate at the start of an expression, it throws an "illegal start of expression" error. The most common reason for this is a missing semi-colon. You might know that every statement in Java ends with a semicolon, but if you forget one, you won't get an error that there is a missing semi-colon at the end of the statement because the compiler doesn't know the end.
When the compiler checks the next statement it sees an illegal start because an earlier statement was not terminated. The bad part is that you can get tens of "illegal start of expression" errors by just omitting a single semi-colon or missing braces, as shown in the following example.
public class Main {
public static void main(String[] args) { count();
public static int count() { return 0; }
}
If you compile this program you will be greeted with several "illegal start of expression" error as shown below:
$ javac Main.java
Main.java:7: error: illegal start of expression
public static int count() {
^
Main.java:7: error: illegal start of expression
public static int count() {
^
Main.java:7: error: ';' expected
public static int count() {
^
Main.java:7: error: ';' expected
public static int count() {
^
Main.java:11: error: reached end of file while parsing
}
^
5 errors
And the only problem is missing curly braces in the main method, as soon as you add that missing closing curly brace in the main method the code will work fine and all these errors will go away, as shown below:
The error message is very interesting, if you look at the first one, you will find that error is in line 7 and the compiler complaining about a public keyword but the actual problem was in line 5 where a closing brace is missing.
From the compiler's view, the public should not come there because it's an invalid keyword inside a method in Java, remember without closing braces main() is still not closed, so the compiler thinks the public keyword is part of the main() method.
Though, when you compile the same program in Eclipse, you get a different error because its the compiler is slightly different than javac but the error message is more helpful as you can see below:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "}" to complete MethodBody
at Main.main(Main.java:4)
In short, the "illegal start of expression" error means the compiler find something inappropriate, against the rules of Java programming but the error message is not very helpful. For "illegal start of expression" errors, try looking at the lines preceding the error for a missing ')' or '}' or missing semicolon.
Also remember, a single syntax error somewhere can cause multiple "illegal start of expression" errors. Once you fix the root cause, all errors will go away. This means always recompile once you fix an error, don't try to make multiple changes without compilation.
Other Java troubleshooting guides you may like:
- How to fix "variable might not have been initialized" error in Java? (solution)
- Could not create the Java virtual machine Invalid maximum heap size: -Xmx (solution)
- How to fix class, enum, or interface expected error in Java? (solution)
- How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory (solution)
- Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger in Java (solution)
- Error: could not open 'C:\Java\jre8\lib\amd64\jvm.cfg' (solution)
- java.lang.OutOfMemoryError: Java heap space : Cause and Solution (steps)
- How to avoid ConcurrentModificationException while looping over List in Java? (solution)