Java Top

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 tomorrow:

>> CHECK OUT THE COURSE

1. Overview

The “illegal start of expression” is a common error we may face at the compile-time.

In this tutorial, we'll see examples that illustrate the main causes of this error and how to fix it.

2. Missing Curly Braces

Missing curly braces may lead to the “illegal start of expression” error. Let's take a look at an example first:

package com.baeldung;

public class MissingCurlyBraces {
    public void printSum(int x, int y) {
        System.out.println("Calculation Result:" + calcSum(x, y));
        
    public int calcSum(int x, int y) {
        return x + y;
    }
}

If we compile the above class:

$ javac MissingCurlyBraces.java
MissingCurlyBraces.java:7: error: illegal start of expression
        public int calcSum(int x, int y) {
        ^
MissingCurlyBraces.java:7: error: ';' expected
        public int calcSum(int x, int y) {
   .....

Missing the closing curly brace of printSum() is the root cause of the problem.

The fix to the problem is simple — adding the closing curly brace to the printSum() method:

package com.baeldung;

public class MissingCurlyBraces {
    public void printSum(int x, int y) {
        System.out.println("Calculation Result:" + calcSum(x, y));
    }
    public int calcSum(int x, int y) {
        return x + y;
    }
}

Before we step forward to the next section, let's review the compiler error.

The compiler reports that the 7th line is causing the “illegal start of expression” error. In fact, we know that the root cause of the problem is in the 6th line. From this example, we learn that sometimes the compiler errors don't point to the line with the root cause, and we'll need to fix the syntax in the previous line.

3. Access Modifier Inside Method

In Java, we can only declare local variables inside a method or constructor. We cannot use any access modifier for local variables inside a method because their accessibilities are defined by the method scope.

If we break the rule and have access modifiers inside a method, the “illegal start of expression” error will be raised.

Let's see this in action:

package com.baeldung;

public class AccessModifierInMethod {
    public void printSum(int x, int y) {
        private int sum = x + y; 
        System.out.println("Calculation Result:" + sum);
    }
}

If we try compiling the above code, we'll see the compilation error:

$ javac AccessModifierInMethod.java 
AccessModifierInMethod.java:5: error: illegal start of expression
        private int sum = x + y;
        ^
1 error

Removing the private access modifier easily solves the problem:

package com.baeldung;

public class AccessModifierInMethod {
    public void printSum(int x, int y) {
        int sum = x + y;
        System.out.println("Calculation Result:" + sum);
    }
}

4. Nested Methods

Some programming languages, such as Python, support nested methods. But, Java doesn't support a method inside another method. 

We'll face the “illegal start of expression” compiler error if we create nested methods:

package com.baeldung;

public class NestedMethod {
    public void printSum(int x, int y) {
        System.out.println("Calculation Result:" + calcSum(x, y));
        public int calcSum ( int x, int y) {
            return x + y;
        }
    }
}

Let's compile the above source file and see what the Java compiler reports:

$ javac NestedMethod.java
NestedMethod.java:6: error: illegal start of expression
        public int calcSum ( int x, int y) {
        ^
NestedMethod.java:6: error: ';' expected
        public int calcSum ( int x, int y) {
                          ^
NestedMethod.java:6: error: <identifier> expected
        public int calcSum ( int x, int y) {
                                   ^
NestedMethod.java:6: error: not a statement
        public int calcSum ( int x, int y) {
                                        ^
NestedMethod.java:6: error: ';' expected
        public int calcSum ( int x, int y) {
                                         ^
5 errors

The Java compiler reports five compilation errors. In some cases, a single error can cause multiple further errors during compile time.

Identifying the root cause is essential for us to be able solve the problem. In this example, the first “illegal start of expression” error is the root cause.

We can quickly solve the problem by moving the calcSum() method out of the printSum() method:

package com.baeldung;

public class NestedMethod {
    public void printSum(int x, int y) {
        System.out.println("Calculation Result:" + calcSum(x, y));
    }
    public int calcSum ( int x, int y) {
        return x + y;
    }
}

5. char or String Without Quotes

We know that String literals should be wrapped in double quotes, while char values should be quoted using single quotes.

If we forget to enclose these in the proper quotes, the Java compiler will treat them as variable names.

We may see “cannot find symbol” error if the “variable” is not declared.

However, if we forget to double-quote a String that is not a valid Java variable name, the Java compiler will report the “illegal start of expression” error.

Let's have a look at it through an example:

package com.baeldung;

public class ForgetQuoting {
    public int calcSumOnly(int x, int y, String operation) {
        if (operation.equals(+)) {
            return x + y;
        }
        throw new UnsupportedOperationException("operation is not supported:" + operation);
    }
}

We forgot to quote the String + inside the call to the equals method, and + is obviously not a valid Java variable name.

Now, let's try compiling it:

$ javac ForgetQuoting.java 
ForgetQuoting.java:5: error: illegal start of expression
        if (operation.equals(+)) {
                              ^
1 error

The solution to the problem is simple — wrapping String literals in double-quotes:

package com.baeldung;

public class ForgetQuoting {
    public int calcSumOnly(int x, int y, String operation) {
        if (operation.equals("+")) {
            return x + y;
        }
        throw new UnsupportedOperationException("operation is not supported:" + operation);
    }
}

6. Conclusion

In this short article, we talked about five different scenarios that will raise the “illegal start of expression” error.

Most of the time, when developing Java applications, we'll use an IDE that warns us as errors are detected. Those nice IDE features can go a long way towards protecting us from facing this error.

However, we may still encounter the error from time to time. Therefore, a good understanding of the error will help us to quickly locate and fix the error.

Java bottom

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 tomorrow:

>> CHECK OUT THE COURSE

Leave a Reply

avatar
  Subscribe  
Notify of