Java Compiler Error – ‘(‘ or ‘[‘ expected


This content was proof-read by Grammarly. You can Try Grammary for FREE

What is the Java Compiler Error ?

error: ‘(‘ or ‘[‘ expected

Reason for the Error

You will likely get this error when you have missed [] when declaring an array in Java. For example, the below code snippet results in the ‘(‘ or ‘[‘ expected error in Java.

public class Main {
    public static void main(String[] args) {
        String[] arry = new String  {"item1", "item2" };
    }
}

There are other reasons why you might this error too. For example, you have either missed ( or [ methos when declaring or calling a method or when calling and returning value form the array index.

How to Fix the Error?

You can fix the error by including the missing ‘(‘ or ‘[‘ as shown in the below code snippet.

public class Main {
    public static void main(String[] args) {
        String[] arry = new String [] {"item1", "item2" };
    }
}
%d bloggers like this: