How to Fix Java Error – Identifier Expected

Posted by Marta on April 13, 2021 Viewed 17461 times

Learn how to fix the java error: identifier expected to get a better understanding of this error and being able to avoid in the future

Card image cap

In this article you will learn how to fix the java error: identifier expected to get a better understanding of this error and being able to avoid in the future.

This error is a very common compilation error that beginners frequently face when starting with java. I will explain what is the meaning of error and how to fix it.

What’s the meaning of Identifier Expected error?

The identifier expected error is a compilation error, which means the code doesn’t comply with the syntax rules of the Java language. For instance, one of the rules is that there should be a semicolon at the end of every statement. Missing the semicolon will cause a compilation error. The identifier expected error is also a compilation error that indicates that you wrote a block of code somewhere where java doesn’t expect it.

Here is an example of piece of code that presents this error:

public class Example {
	System.out.println("Hello");
}

If you try to compile this class, using the javac command in the terminal, you will see the following error:

javac Example.java 

Output:

Example.java:5: error: <identifier> expected
    System.out.println("Hello");
                      ^
Example.java:5: error: illegal start of type
    System.out.println("Hello");
                       ^

This error is slightly confusing because it seems to suggest there is something wrong with line 2. However what it really means is that this code is not in the correct place.

How to Fix it

We have seen what the error actually means, however how could I fix it? The error appears because I added some code in the wrong place, so what’s the correct place to right code? Java expects the code always inside a method. Therefore, all necessary to fix this problem is adding a class method and place the code inside. See this in action below:

public class Example {

    public void print() {
        System.out.println("Hello");
    }
}

The code above will compile without any issue.

Another example

Here is another example that will return the identifier expected error:

public class Example {

    public void print() {
        System.out.println("Hello");
    }

    Example e = new Example();
    e.print(); // Here is the error
}

Output:

Example.java:10: error: <identifier> expected
    e.print();
           ^

As before, this compilation error means that there is a piece of code: e.print() that is not inside a class method. You might be wondering, why line 7 ( Example e = new Example(); ) is not considered a compilation error? Variable declarations are allowed outside a method, because they will be considered class fields and their scope will be the whole class.

Here is a possible way to fix the code above:

public class Example {

    public void print() {
        System.out.println("Hello");
    }

    Example e = new Example();

    public void method2(){
        e.print();
    }
}

The fix is simply placing the code inside a method.

Conclusion

To summarise, this article covers how to fix the identifier expected java error. This compilation error will occur when you write code outside a class method. In java, this is not allow, all code should be placed inside a class method.

In case you want to explore java further, I will recommend the official documentation

Hope you enjoy the tutorial and you learn what to do when you find the identifier expected error. Thanks for reading and supporting this blog.

Happy coding!

More Interesting Articles

How to shuffle a string in java

How to open a web browser in python

Runnable vs Callable – Find out the differences

What is a mutator method in Java?

Project-Based Programming Introduction

Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications

100% Recommended book for Java Beginners

Unique lay-out and teaching programming style helping new concepts stick in your memory

90 Specific Ways to Write Better Python

Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples

Grow Your Java skills as a developer

Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.

Write Code as a Professional Developer

Excellent read for anyone who already know how to program and want to learn Best Practices

Every Developer should read this

Perfect book for anyone transitioning into the mid/mid-senior developer level

Great preparation for interviews

Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java