Home » Identifier Expected Error in Java

Identifier Expected Error in Java

by Online Tutorials Library

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

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

Identifier Expected Error in Java

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

Output:

tutoraspire  

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

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

Identifier Expected Error in Java

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

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

Identifier Expected Error in Java

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

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

IdentifierErrorExample3.java

Let’s compile the above code.

Identifier Expected Error in Java

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

Output:

Identifier Expected Error in Java


Next TopicJava Generate UUID

You may also like