[Solved] Error: Identifier expected in Java

In this post, I will be sharing how to fix the error "identifier expected in Java". This compilation error is mostly faced by Java beginners. This error generally occurs when you place the code inside the class instead of the method. Before moving on to the solution, we will produce the error first.

Read Also: Error: Could not find Java SE Runtime Environment

[Fixed] Error : Identifier expected


Example 1: Producing the error by using an extra curly brace


Consider the following code:

public class HelloWorld {
   public static void main(String args[]) {
System.out.println("Alive is Awesome");}
System.out.println("Love Yourself"); } }


If you compile the above code using the javac command:

javac HelloWorld.java

then you will find the following compilation error:

Output:
/HelloWorld.java:4: error: <identifier> expected
System.out.println("Love Yourself");
                              ^
/HelloWorld.java:4: error: illegal start of type
System.out.println("Love Yourself");
                               ^
/HelloWorld.java:6: error: class, interface, or enum expected
}
^
3 errors

Explanation


If you observe the code, then you will find there is an extra curly brace, since the code is not properly indented, it is difficult to view. The above error can be fixed by removing the extra curly brace.

Solution



public class HelloWorld {
   public static void main(String args[]) {
            
System.out.println("Alive is Awesome");
System.out.println("Love Yourself"); } }


Output:
Alive is Awesome
Love Yourself


Example 2: Producing the error by placing code inside the class



public class HelloWorld {
System.out.println("Alive is Awesome");
}


If you compile the above code using the javac command:

javac HelloWorld.java

You will get the following error:

/HelloWorld.java:2: error: <identifier> expected
System.out.println("Alive is Awesome");
                              ^

/HelloWorld.java:2: error: illegal start of type
System.out.println("Alive is Awesome");
                               ^
2 errors


Explanation


We are getting <identifier> expected error because we have placed the code inside the class not inside the method. Let's solve this issue by placing the code inside the main method.

Solution


public class HelloWorld {
   public static void main(String args[]) {
System.out.println("Alive is Awesome");
} }

Output:
Alive is Awesome


Example 3: Producing the error by missing comma in enum


Consider the following code:

public class MissingComma {
    public enum Company {
APPLE;
GOOGLE; } public static void main(String args[]){ for(Company c : Company.values()) System.out.println(c); } }

If you compile the above code using the javac command i.e javac MissingComma.java then you will face the following compilation error:

Output:
/MissingComma.java:4: error: <identifier> expected
GOOGLE;
                ^
1 error

Explanation


We are getting <identifier> expected error because we have missed comma in the enum types. Let's solve this issue by placing the comma inside the enum Company.

Solution



public class MissingComma {
    public enum Company {
APPLE,
GOOGLE; } public static void main(String args[]){ for(Company c : Company.values()) System.out.println(c); } }


Output:
APPLE
GOOGLE

That's all for today, please mention in the comments in case you are still facing the error: identifier expected in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry