Identifier Expected Error in Java

An identifier expected error is a very common error faced by beginners. In this section, we will discuss what is identifier expected error , the reasons to occur errors, and how to fix the identifier expected error in Java. Before moving to the error first we will understand what are identifiers in Java .

Identifiers in Java are symbolic names used for identification. They can be a class name, variable name, method name, package name, constant name , etc. However, In Java , there are some reserved words that cannot be used as an identifier such as int, const, new, double, enum, etc.

What is an identifier expected error?

It is a very common compilation error that occurs at compile time.

Let's consider the following Java program.

IdentifierError.java

public class IdentifierError System.out.println("javatpoint");

When we try to compile the above program, we get the following error.

The code looks fine but not so. Because the print statement is not a proper place. It should be inside a method/ block. Let's wrap the code inside a method and then compile and run.

IdentifierError.java

public class IdentifierError public static void main(String args[]) System.out.println("javatpoint");

Output:

javatpoint

Reasons to Occur Error

There may be the following reasons to occur the error:

  • It occurs when the code does not comply with the Java syntax rules.
  • A block of code directly written in the class body instead of inside a method or block.
  • There may be extra curly braces.
  • The code is not at the proper place.
  • Every statement must have a semicolon at the end.
  • How to fix/ resolve errors?

  • Do not forget to put a semicolon at the end of the statement.
  • Do not put code directly inside the class body.
  • Calling of methods must be inside a method, constructor, or static initializer.
  • Write a block of code at the proper place.
  • Remove extra curly braces.
  • IdentifierErrorExample1.java

    public class IdentifierErrorExample1 public static void main(String args[]) System.out.println("Python"); System.out.println("MySql"); System.out.println("Java");

    Let's compile the above code. We get the <identifier> expected error.

    Observe the above code, we get there is an extra curly brace that is the reason to generate an error. The error can be fixed by removing an extra brace at line 6.

    The error also occurs when we put semicolon instead of comma while defining values in enum. For example, consider the following code.

    IdentifierErrorExample2.java

    public class IdentifierErrorExample2 public enum Vegetables eggplant, tomato, broccoli; public static void main(String args[]) for(Vegetables veg : Vegetables.values()) System.out.println(veg);

    Let's run the above code. we get the identifier expected error.

    To fix the error, remove semicolons from the enum values.

    public enum Vegetables eggplant, tomato, broccoli;

    Sometimes the error may be much larger. Consider the following code.

    IdentifierErrorExample3.java

    import java.util.Arrays; public class IdentifierErrorExample3 int[] primes = {17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73}; int max_val; max_val = nums[0]; for (int i = 1; i < primes.length; ++i) if (primes[i] > max_val) max_val = primes[i]; System.out.println("Primes Numbers are: " + Arrays.toString(primes)); System.out.println("Largest Prime is: " + max_val);

    Let's compile the above code.

    We get too many errors because some statements directly write inside the class body. To resolve the error, write entire the block of code inside a method and then compile and run.

    IdentifierErrorExample4.java

    import java.util.Arrays; public class IdentifierErrorExample4 public static void main(String args[]) int[] primes = {17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73}; int max_val; max_val = primes[0]; for (int i = 1; i < primes.length; ++i) if (primes[i] > max_val) max_val = primes[i]; System.out.println("Primes Numbers are: " + Arrays.toString(primes)); System.out.println("Largest Prime is: " + max_val);

    Output:

    Next TopicJava Generate UUID