Illegal start of expression in Java

The illegal start of an expression is a java error during the compile-time with the “javac” compiler. When the compiler detects that a statement does not follow the syntax rules of the java programming language, an illegal start of expression error message is displayed. These errors can occur due to so many reasons. In most cases, they are straightforward to solve as long as you can locate the line of code with the error. It can be as simple as caused by an omitting closing or opening bracket or a missing semicolon at the end of a line.

Illegal start of expression in Java

The java compiler compiles the code from top to bottom, left to right. A common question is why does it not display the error as “illegal end of an expression” when one omits a semicolon at the end of a line? The compiler does not know the ending. When it checks the following statement and the earlier statement was not terminated, it sees an illegal start of expression.

Example:

public class equals{

     public static void main(String []args)
     
     {
         if( 5 => 2)
         {
             System.out.println("Hello World");
             
         }
     }
        
}

Output:

$javac equals.java
equals.java:6: error: illegal start of expression
         if( 5 => 2)                ^
1 error

In the above example, an illegal start of an expression is thrown since we started with (=>) rather than writing the expression as (5 >=2). It might be hard for you to notice this hence the necessity of improving your daily programming skills. With that said, let’s look at some scenarios where you can encounter this error and solve it.

Use of access modifiers with local variables

An access modifier sets the scope of methods constructors or classes and other members. Unlike global variables that can be used anywhere in the class, local variables can only be used within a block. They have a limited scope hence cannot be used anywhere in the class. Assigning an access modifier changes the scope of a variable during declaration.
Example: Defining a global variable as ‘public student; ‘ means the variable is public and can be used anywhere in a class. In an instance where this was a local variable, an illegal start of an expression will be displayed.

public class numbers{

     public static void main(String []args)
     
     {
        public int number = 1;
     }     

}

Output:

$javac numbers.java
numbers.java:6: error: illegal start of expression
        public int number = 1;
        ^
1 error

In the above example, int number is a local variable and can only be used within the primary method. Assigning it a public access modifier tries o change its scope, and hence we get a compile-time error if an illegal start of an expression.

To solve this, you will need to take the variable declaration outside the primary method, making it a global variable, or remove the public access modifier.

Method inside of another method

Java does not permit the definition of a method inside another method. This throws an illegal start of expression.

public class number{

     public static void main(String []args)
     {
               int num = 5;
	 public void Method2()
	 { 
	          System.out.println("it is a method inside a method.");
	       }
	    }
}

Output:

$javac number.java
number.java:7: error: illegal start of expression
	 public void Method2()
	 ^
number.java:7: error: illegal start of expression
	 public void Method2()
	        ^
number.java:7: error: ';' expected
	 public void Method2()
	                    ^
3 errors

In the above code, Method2 is inside the main method. However, since java syntax rules of programming do not allow this, it throws an illegal start of expression.

To solve this, we make sure you do not use a method inside another method in Java. The best practice would be to declare another method outside the main method and call it in the main as per your requirements.

Class inside a method must not have a modifier

Java does not allow a method inside a method, but funny enough, it will enable one to write a class within a method. This will be a local class that will only be used inside that method. Assigning an access modifier tries to change the scope of the inner class to be accessible outside the method resulting in an illegal start of expression.

public class University {
  
    	    public static final void main(String args[]){        
	     public class department { }
	    }
	}

In the above example, the class ‘University’ is defined within the main method is within the class called ‘department’. Therefore, using the public access modifier generates the error.

$javac University.java
University.java:4: error: illegal start of expression
	     public class department { }
	     ^
1 error

An inner class cope is only within a method. To solve this, make sure inner classes do not contain an access modifier. You can also define a class within a class but outside a method.

Nested methods

Nesting is the process of one programming construct including it within another.  Java does not allow using a method inside another method and throws an illegal start of expression. These limitations, however, are not there in some programming languages such as python.

public class University
{
	public void print ()
	{
		String student = "Method Inside Method";
		public void print String (String st)
		{
			System.out.print(s);
		}
	}
}

Output:

$javac University.java
University.java:6: error: illegal start of expression
		public void print String (String st)
		^
University.java:6: error: illegal start of expression
		public void print String (String st)
		       ^
University.java:6: error: ';' expected
		public void print String (String st)
		                 ^
University.java:6: error: ')' expected
		public void print String (String st)
		                                ^
University.java:6: error: illegal start of expression
		public void print String (String st)
		                                   ^
University.java:6: error: ';' expected
		public void print String (String st)
		                                    ^
University.java:11: error: class, interface, or enum expected
}
^
7 errors

String without a double quote or Character without single quote

A character is a primitive data type, and a String is a class. String variables and character variables must be within the double-quotes and single quotes, respectively. Initializing a string or char string variable without his results into the compile-time error.  If you lack the quotes, the java compiler considers them as names.

Character without single quote

public class character {
  public static void main(String[] args) {
    char symbol = /;
    
      System.out.println(symbol);
  }
}

Output:

$javac character.java
character.java:3: error: illegal start of expression
    char symbol = /;
                  ^
character.java:3: error: illegal start of expression
    char symbol = /;
                   ^
2 errors

String without double quote

public class sample {
  public static void main(String[] args) {
    String website = Codeunderscored ;
    
      System.out.println(website);
  }
}

With double quotes:

public class sample {
  public static void main(String[] args) {
    String website = "Codeunderscored" ;
    
      System.out.println(website);
  }
}

Output:

$javac sample.java
$java -Xmx128M -Xms16M sample
Codeunderscored

Char definition contains a (‘)  Whereas String use (“).

Missing Curly braces

In the Java syntax rules of programming, every block of code must start with a   curly brace and end with a curly brace. Otherwise, if you miss any of the braces, the compiler will not identify the start of the block or the ending. This results in a compile-time error.

  public class sample {
  public static void main(String[] args) {
    String website = "Codeunderscored" ;
    
      System.out.println(website);
  }

In some compilers, this error will be displayed as error: reached end of file while parsing. This means the compiler is unable to locate the end of the statement.

$javac sample.java
sample.java:7: error: reached end of file while parsing
}
 ^
1 error

Conlusion

Programming errors are always invertible. You cannot do away with them altogether. Developing your skills in programming will, however, reduce these errors and make your debugging skills better. In this tutorial, we have covered the illegal start of an expression and solve it in Java. Since Java does not report the illegal end of an expression to fix the illegal start of an expression always looks for a mistake in the line just above the line mentioned in the error message. A single syntax error can sometimes cause multiple “Illegal start of expression” errors in some cases. Evaluating the root cause solves the error at once.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *