Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

error
compiler
expression
resolve
java

How to resolve the "java illegal start of expression" error

Educative Answers Team

The "illegal start of expression" error in Java can be caused by any one of the following three reasons:

svg viewer
$1.$ Missing an opening or closing curly bracket for a code block (i.e., function, conditional code, loop, etc):
public void foo(){
    System.out.println("No closing bracket for this function.");

This has to be resolved manually by looking for a pair of curly brackets for each code block and completing the missing ones.

$2.$ Declaring a function inside another function:
public void foo(){
    // Declaring bar() inside foo():
    private int bar(){
        return 10;
    }
}

Move the function declared inside (i.e., bar()) to an appropriate place outside the outer function (i.e., foo()).

$3.$ Using an access specifier with a function's local variable:
private int foo(){
    // Writing 'public' with 'int':
    public int x = 5;
    return x;
}

The local variables of a function are only visible inside it and their scope cannot be widened or narrowed. Remove the access specifiers of the variables to resolve this error.

RELATED TAGS

error
compiler
expression
resolve
java
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring