Core Java

How to fix an illegal start of expression in Java

Have you ever come across the error illegal start of expression in Java and wondered how to solve it? Let’s go through the post and study how we address the Illegal start of expression Java error.

This is a dynamic error, which means that the compiler finds something that doesn’t follow the rules or syntax of Java programming. Beginners mostly face this bug in Java. Since it is dynamic, it is prompted at compile time i.e., with the javac statement.

This error can be encountered in various scenarios. The following are the most common errors. They are explained on how they can be fixed. 

1. Prefixing the Local Variables with Access Modifiers

Variables inside a method or a block are local variables. Local variables have scope within their specific block or method; that is, they cannot be accessed anywhere inside the class except the method in which they are declared. Access modifiers: public, private, and protected are illegal to use inside the method with local variables as its method scope defines their accessibility.

This can be explained with the help of an example:  

Class LocalVar {
public static void main(String args[])
{
int variable_local = 10
}
}
illegal start of expression in Java - Access Modifiers
Using the modifier with the local variable would generate an error

2. Method Inside of Another Method

A method cannot have another method inside its scope. Using a method inside another method would throw the “Illegal start of expression” error. The error would occur irrespective of using an access modifier with the function name. 

Below is the demonstration of the code: 

Class Method
{
public static void main (String args[])
{
public void calculate() { } 
}
}
illegal start of expression in Java - Definition of a method inside
Definition of a method inside and another method is illegal
Class Method
{
public static void main (String args[])
{
void calculate() { } 
}
}
illegal start of expression in Java
The error doesn’t depend on the occurrence of modifier alone

3. Class Inside a Method Must Not Have Modifier

Similarly, a method can contain a class inside its body; this is legal and hence would not give an error at compile time. However, make note classes do not begin with access modifiers, as modifiers cannot be present inside the method.

In the example below, the class Car is defined inside the main method; this method is inside the class Vehicle. Using the public modifier with the class Car would give an error at run time, as modifiers must not be present inside a method.

class Vehicle
{
public static final void main(String args[])
{
public   class Car { }
}
}
illegal start of expression in Java - Declaring a class with a modifier
Declaring a class with a modifier, inside the method is not allowed

4. Missing Curly “{}“ Braces

Skipping the curly braces of any method block can result in having an “illegal start of expression” error. The error will occur because it would be against the syntax or against the rules of Java programming, as every block or class definition must start and end with curly braces. The developer might also need to define another class or method depending on the requirement of the program. Defining another class or method would, in turn, have modifiers as well, which is illegal for the method body.

In the following code, consider the class Addition, the method main adds two numbers and stores them in the variable sum. Later, the result is printed using the method displaySum. An error would be shown on the terminal as the curly brace is missing at the end of the method main.

public class Addition
{
static int sum;
public static void main(String args[])
{
int x = 8;
int y= 2;
sum=0;
sum= x + y;
{
System.out.println("Sum = " + sum);
}
}
illegal start of expression in Java - Missing curly braces
Missing curly braces from the block definition causes errors.

5. String Character Without Double Quotes “” 

Initializing string variables without the double quotes is a common mistake made by many who are new to Java, as they tend to forget the double quotes and later get puzzled when the error pops up at the run time. Variables having String data type must be enclosed within the double quotes to avoid the “illegal start of expression” error in their code.

The String variable is a sequence of characters. The characters might not just be alphabets, they can be numbers as well or special characters like @,$,&,*,_,-,+,?, / etc. Therefore, enclose the string variables within the double quotes to avoid getting an error.

Consider the sample code below; the missing quotes around the values of the variable operator generates an error at the run time.

import java.util.*;
public class Operator
{
public static void main(String args[])
{
int a = 10;
int b = 8;
int result =0; 
Scanner scan = new Scanner(System.in);
System.out.println("Enter the operation to be performed");
String operator= scan.nextLine();
if(operator == +)
{
   result = a+b;
}
  else 
   if(operator == -)
{
    result = a-b;
   }
  else
{
System.out.prinln("Invalid Operator");
}
  System.out.prinln("Result = " + result); 
}
String values must be enclosed in double-quotes to avoid above mentioned error

6. Summary

To sum up, the “Illegal start of expression” error occurs when the Java compiler finds something inappropriate with the source code at the time of execution. To debug this error, try looking at the lines preceding the error message for missing brackets, curly braces, or semicolons and check the syntax.

Useful tip: Remember, in some cases, a single syntax error sometimes can cause multiple “Illegal start of expression” errors. Therefore, evaluating the root cause of the error and always recompiling when you fix the bug that means avoiding making multiple changes without compilation at each step.

7. Download the Source Code

Download
You can download the full source code of this article here: How to fix an illegal start of expression in Java

Last updated on Jan. 10th, 2022

Simran Arora

Simran, born in Delhi, did her schooling and graduation from India in Computer Science. Curious and passionate about technology urged her to study for an MS in the same from the renowned Silicon Valley, California USA. Graduated in 2017, she now works as a freelance technical content developer.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
MarcelMA
MarcelMA
4 years ago

So your first example is not the one that shows the error?

Back to top button