Important Java Concept

Fixing Illegal Start Of Expression In Java

We will learn about what is an illegal start of expression, and the types of illegal expressions.       

An illegal start of expression is an error that occurs when the compiler finds something inappropriate in the java code during the execution time. Some simple mistakes most beginners make that will cause this type of error are missing curly braces, missing brackets, missing semicolon, syntactic errors, etc. Let us understand more about the illegal start of expression errors in java.

Access Modifier in a Method

Access modifiers (or) Access specifiers is a keyword that specifies how to access the members of a class or a class itself. There are four access specifiers available in java. They are private, public, protected, and default.

class ex{
  public static void main(String[] args){
  public int x = 3;
  System.out.println(x);
          }
 }

Output:

illegal.java:3: error: illegal start of expression
        public int x = 3;
        ^
       1 error

Explanation

Here we can only declare a local variable inside a method. We cannot use any access specifier for a local variable because the accessibility of that local variable is defined by the method scope.

In the above program, the x variable was declared as public inside the main method which is why during the execution of the code we encountered an illegal start of expression error.

Solution

The above error can be deleted by removing the access specifier given to the local variable ‘x’.

class ex{
  public static void main(String[] args){
    int x = 3;
    System.out.println(x);
     }
 }

Nested Methods

A method inside another method is known as a nested method. Java doesn’t support nested methods.

class ex1{
 public static void main(String[] args){
 public int add(){
  int x = 3;
  int x = 4;
  return x+y;
     }
   }
}

Output

illegal.java:3: error: illegal start of expression
public int add(){
^
  illegal.java:3: error: ';' expected
  public int add(){
                       ^
  2 errors

Explanation

 In the above java code, add() is a method that is declared inside the main method, as java doesn’t support nested methods here an illegal start of expression error has occurred.

Solution

The above error can be deleted by removing the second method(i.e., add()).

class ex1{
  public static void main(String[] args){
   int x = 3;
   int y = 4;
   int sum = x+y;
 }
}

Missing Curly Braces

Missing curly braces may lead to an illegal start of expression error. An opened curly brace must be closed.

public class ex2{
  public void add(int x,int y){
   System.out.println(sum(x,y));

  public int sum(int x, int y ){
   return x+y;  
                    }                       
             }

Output

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

Explanation

In the above code, we have missed keeping a closed curly brace to a method, which is why we have encountered an error called the illegal start of expression.

Solution

The above error can be eliminated by adding a curly brace at the appropriate location.

public class ex2{
 public void add(int x,int y){
   System.out.println(sum(x,y));
  }
  public int sum(int x, int y ){
   return x+y;  
   }                       
}

String without double quotes

A value is said to be a string if it was enclosed in between double-quotes.

class ex3{
  public static void main(String[] args){
   int n1 = 10;
   int n2 = 12;
   int sum = 0;
   Scanner sc = new Scanner(System.in);
   String str = sc.nextLine();
   if(str == +){
    sum = n1+n2;
  }
 }
}

Output

illegal.java:8: error: illegal start of expression
if(str == +){
              ^
1 error

Explanation

In the above java code, String wasn’t represented in double-quotes, this is why it has raised an illegal start of expression error.

Solution

The above error can be eliminated by placing + in double quotes.

class ex3{
  public static void main(String[] args){
   int n1 = 10;
   int n2 = 12;
   int sum = 0;
   Scanner sc = new Scanner(System.in);
   String str = sc.nextLine();
   if(str == “+”){
    sum = n1+n2;
   }
  }
}

Class Inside a Method must not have a Modifier

A class that was declared inside a method should have any access modifier or access specifier to it.

class ex4{
  public static void main(String[] args){
  public class hello{}
 }
}

Output

illegal.java:3: error: illegal start of expression
public class hello{}
^
1 error

Explanation

In the above java code, an access specifier public was used to a class that was declared inside the main method which is why an illegal start of expression error was raised.

Solution

The above error can be eliminated by removing the access specifier to the class which was declared inside the main method.

class ex4{
  public static void main(String[] args){
  class hello{}
 }
}

Conclusion

We have learned about what is an illegal start of expression error and all the situations where there is a possibility of occurring illegal start of expression errors and appropriate solutions to avoid these errors.

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?