Java Error: illegal start of expression

Expressions are essential building blocks of any Java program . One of the most common Java error illegal start of expression , is a compile time error when the compiler encounters an inappropriate statement in the source code. This error can be encountered in various scenarios . In this course, we'll see examples that elaborate the main causes of this error and how to fix it.
  1. Missing curly braces
  2. Method Inside of Another Method
  3. Access Modifier Inside Method
  4. Char or String Without Quotes

Missing curly braces

illegal start of expression Missing curly braces In a Java program, curly braces enclose meaningful units of code. When you open a curly brace it means that you open a new scope (usually it's a nested scope). One important thing to understand is that a variable that you'll define inside this scope (which end where you put the closing curly brace) will not be recognized outside of the scope. So, it is important to note that missing curly braces may lead to the illegal start of expression error. example
public class sample { public static void main(String[] args) { System.out.println("Hello"); public void myMessage(){ System.out.println("Hai"); } }
output
sample.java:6: error: illegal start of expression public void myMessage(){ sample.java:6: error: illegal start of expression public void myMessage(){ sample.java:6: error: ';' expected public void myMessage(){ sample.java:9: error: reached end of file while parsing } 4 errors
Missing the closing curly brace of main() method is the root cause of the problem. In order to fix this problem, adding the closing curly brace to the main() method.
public class sample { public static void main(String[] args) { System.out.println("Hello"); } public void myMessage(){ System.out.println("Hai"); } }

Method Inside of Another Method (Nested Methods)

Java does not support "directly" nested methods . (Most functional languages do though, including some JVM languages such as Scala and Clojure ). Nested methods would not be meaningful in Java, because methods do not have scope: they have a containing class . From the perspective of the Java runtime, this means it doesn't have to apply scoping rules to resolve method names: it simply looks them up in the inheritance hierarchy of the instance (or class, in the case of static methods) which contains them. So, using a method inside another method would in Java throw the Illegal start of expression error. example
public class sample { public static void main(String[] args) { System.out.println("Hello"); } public void outerMethod(){ public void innerMethod(){ System.out.println("Hai"); } } }

In the above example, we can see that the method innerMethod() is placed inside another method outerMethod().

output
sample.java:7: error: illegal start of expression public void innerMethod(){ sample.java:7: error: illegal start of expression public void innerMethod(){ sample.java:7: error: ';' expected public void innerMethod(){ 3 errors
In java 8 and newer version you can achieve nested methods by lambda expression .

Access Modifier Inside Method

Access Modifier Inside Method illegal start of expression Access modifiers in Java helps to restrict the scope of a class , constructor , variable , method or data member. Since the scope of the local variables belong to the method/block/constructor/ these are not accessed from outside, therefore, having access specifiers like public, private, protected (which allows access outside of the current context) makes no sense. If we break the rule and have access modifiers inside a method, the illegal start of expression error will be raised. example
public class sample { public void add(int x, int y){ private int sum = x+y; System.out.println("Result : " + sum); } }
output
sample.java:4: error: illegal start of expression private int sum = x+y; 1 error
You can fix this error by removing the private access modifier from "private int sum".

Char or String Without Quotes

In Java, char is a primitive data type whereas String is a class. We define char in java program using single quote (') whereas we can define String in Java using double quotes ("). If we forget to enclose these in the proper quotes, the Java compiler will treat them as variable names . So, if we forget to add quotes to char or string is not a valid Java variable name, then the Java compiler will report the illegal start of expression error. example
public class sample { public static void main(String[] args) { char symb = '/'; if (symb != (+)){ System.out.println("Not..."); } } }
output
sample.java:5: error: illegal start of expression if (symb != (+)){ 1 error

Here, we forgot to add quotes + within single quotes.


single-quotes illegal start of expression

The solution to the problem is simple — wrapping + in single-quotes.

public class sample { public static void main(String[] args) { char symb = '/'; if (symb != ('+')){ System.out.println("Not..."); } } }