Illegal Start of Expression in Java
The illegal start of expression is a common error encountered when working with Java. It is a dynamic error and occurs during compile-time when we use the javac command. We can easily fix it if we know the cause of the error.
In this tutorial, we will learn the reasons behind this error and how to resolve it.
Missing Curly Braces
The illegal start of expression error can occur if we omit an opening or closing curly brace. A missing curly brace will violate the syntax of Java programming, and the code won't compile. The following code shows a simple example of this scenario.
public class Demo
{
public static void print()
{
System.out.print("A curly brace in missing!");
}
public static void main(String[] args)
{
print();
}
}
When we try to compile the above code using javac, we will get the following result.
To fix the error, add the curly brace at the correct location.
Access Modifiers Inside a Method
Variables inside any method or block are called local variables, and their scope is limited to that block. We cannot use access modifiers—like private, protected, or public—with local variables.
If this rule is violated, then the compiler returns an illegal start of expression error. The following code won't compile because we have used the protected access modifier inside the print() method.
public class Demo
{
public static void print()
{
protected String str = "protected access modifier";
}
public static void main(String[] args)
{
print();
}
}
Output:
Removing the protected access modifier will fix the above code.
Nested Methods
Unlike some other programming languages like Python, Java does not support nested methods or methods inside a method. This will result in an illegal start of expression error. The following code demonstrates this.
public class Demo
{
public void print()
{
String str = "Method Inside Method";
public void printString(String s)
{
System.out.print(s);
}
}
}
Output:
Class Inside a Method
Java allows us to write a new class inside a method, but the class will be local to the method. These classes are called Method-Local Inner Classes, and their scope is restricted to just the method. But note that this inner class should not begin with any access modifiers. Methods cannot contain any access modifier. For example, in the code below, the print() method has an inner class(ClassThatPrints) inside it with a public modifier. This will result in the illegal start of expression error.
public class Demo
{
public void print()
{
public class ClassThatPrints
{
public void printFromClass()
{
System.out.print("Hello, Java");
}
}
}
}
Output:
String or Char without Quotes
Strings and char types must be enclosed in quotes. Strings or characters can also contain special characters or operators. So quotes are important to recognize them as strings and not as special characters.
Then an illegal start of expression error occurs If we forget the quotes and the string doesn't contain a valid variable name. For example, in the code below, we didn't enclose the asterisk(*) in quotes. This will make the compiler throw the illegal start of expression error.
public class Demo
{
public static void main(String[] args)
{
String symbol = "*";
int num1 = 5, num2 = 10;
int result;
if(symbol == "/")
result = num2 / num1;
if(symbol == *)
result = num2 * num1;
}
}
Output:
Summary
In this tutorial, we learned the reasons behind the illegal start of expression error. Usually, it is pretty easy to resolve this error. Sometimes this error can also cause multiple other errors in our code. So it is important to look for the root cause of the error. The compiler will sometimes not point to the root cause of the error, so we should fix the code in the preceding lines. Most of the IDEs warn us about the errors before we run our code. But it is important to understand the reasons behind them.