3 ways to fix "illegal start of expression" error in java

"illegal start of expression" error is one of the most common errors, java beginners face. This error arises during compile time i.e running javac command. Usually , java compiler compiles from top to bottom and left to right in a source code file. There are 3 ways in which we can get "illegal start of expression" error.

1. Missing curly braces
2. Method inside method
3. public, protected or private access modifier inside method

Read Also :  Guide to the Java Constructor


Let's check them out one by one.

1. Missing curly braces

"illegal start of expression" arises if you forgot to add curly braces. In the below example, I do not add the closing curly  brace }  of the main method.


public class JavaHungry {
    public static void main(String args[])
    {
        int x=10;
        int y=25;
        int z=x+y;

        System.out.println("Sum of x+y = " + z);
        
    public void anotherMethod()
    {
        System.out.println("another Method");
    }
}
 


Output:


/JavaHungry.java:9: error: illegal start of expression
    public void anotherMethod()
    ^
/JavaHungry.java:9: error: illegal start of expression
    public void anotherMethod()
           ^
/JavaHungry.java:9: error: ';' expected
    public void anotherMethod()
                             ^
/JavaHungry.java:14: error: reached end of file while parsing
}
 ^
4 errors 

If you are using eclipse ide then you will get below error for the above code :

Syntax error, insert “}” to complete MethodBody

If you add closing curly brace to the main method , then the above code will work fine .

2. Method inside Method 

In java , one method can not be inside the another method. If you try to put it, then you will get illegal start of expression error as shown below:

public class JavaHungry {
    public static void main(String args[])
    {        
       public void anotherMethod()
      { 
         System.out.println("another Method");
      }
    }
}

Output :

/JavaHungry.java:4: error: illegal start of expression
       public void anotherMethod()
       ^
/JavaHungry.java:4: error: illegal start of expression
       public void anotherMethod()
              ^
/JavaHungry.java:4: error: ';' expected
       public void anotherMethod()
                                ^
3 errors
 


3. Public, private or protected access modifier inside method

Variable inside the method is called local variable. In case of local variable you can not use any access modifier.
An access modifier is not allowed inside the method because its accessibility is defined by its method scope.

public class JavaHungry {
    public static void main(String args[])
    {        
       public int localVariable = 10;
    }
}


Output

/JavaHungry.java:4: error: illegal start of expression
       public int localVariable = 10;
       ^
1 error
 
One solution is to move the declaration of local variable outside the method.

If you run the above code in the eclipse ide, you will get the following error as shown in the image:

illegal start of expression


Illegal modifier for parameter localVariable; only final is permitted

Eclipse ide is providing better information about the error then the java compiler.

That's all for the day. Today I discussed various ways to get Illegal start of expression error and how to fix it. Please post the code in the comments if you are facing the same error.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry