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.



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.

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.


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


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.



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.



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.
 (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About