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.- Missing curly braces
- Method Inside of Another Method
- Access Modifier Inside Method
- Char or String Without Quotes
Missing curly braces

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

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.

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...");
}
}
}
Related Topics
- Reached end of file while parsing
- Unreachable statement error in Java
- Int Cannot Be Dereferenced Error - Java
- How to fix java.lang.classcastexception
- How to fix java.util.NoSuchElementException
- How to resolve java.net.SocketException: Connection reset
- Non-static variable cannot be referenced from a static context
- Error: Could not find or load main class in Java
- How to Handle OutOfMemoryError in Java
- How to resolve java.net.SocketTimeoutException
- How to handle the ArithmeticException in Java?