Java: Identifier expected
You can’t call methods outside a method. Code like this cannot float around in the class.
You need something like:
or inside a constructor:
input.name() needs to be inside a function; classes contain declarations, not random code.
Try it like this instead, move your myclass items inside a main method:
![]()
I saw this error with code that WAS in a method; However, it was in a try-with-resources block.
Identifier Expected Error in Java
In this article, we will learn about Java’s <identifier> expected error.
Please enable JavaScript
Understanding <identifier> expected Error in Java
The <identifier> expected is the most common Java compile-time error faced by novice programmers. This error occurs when a method parameter does not have a data type or name declared or when an expression statement is written outside a method, a constructor, or an initialization block.
In Java, an identifier is a sequence of one or more characters where the 1st character must be a letter or $ or _ , and the subsequent characters can be letters, digits, $ or _ .
These identifiers are used to name a variable, a method, a class, a package, an interface, etc.
When the Java code is compiled initial phase involves lexical analysis of the program. Here the program is categorized into tokens, and all the tokens, including identifiers, are verified against a set of grammar rules.
And whenever as per grammar rules, an identifier is expected, but it’s not present, and something else is found in its place compiler raises the <identifier> expected error. Angle brackets here signify a reference to the token objects.
Let’s look at some examples to understand it better now.
Example 1: When Parameters in a Method Are Missing Data Type or Name
Data of the parameter is missing
If we observe the error above, we see that variable x is missing the data type. If we give the data type for it, it will work properly.
The parameter’s data type is the identifier; the variable name is missing here.
If we observe the error above, we can clearly see that we specified the data type but didn’t attach any variable name.
Example 2: When the Expression Statements Are Misplaced
The compiler raises an error when an expression statement is written outside a method, a constructor or an initialization block <identifier> expected .
If we observe the error above, the error occurs because the statements are not enclosed within a function or a constructor. Let’s write these statements inside a method named print .
Example 3: When Declaration Statements Are Misplaced
This is rare but not unheard of when working with try-with-resources statements. All these statements require that any closable resource be declared immediately after the try keyword.
If the resource variable is declared outside the try-with-resources statement, then the <identifier> expected error might be raised by the compiler (as compiler to compiler, this may vary).
Example code: Here, the closable resource is BufferedReader .
The error will be resolved if we declare the BufferedReader obj inside the try block.
Conclusion
In this article, we learned about why the <identifier> expected error had been raised by the compiler and how it’s the most common compile-time error faced by newbies. We also looked at different examples analyzing the cases when this error was raised.
A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.
How to Handle the <Identifier> Expected Error in Java

By definition, an identifier in Java is a sequence of one or more characters, where the first character must be a valid first character (letter, $, _) and each subsequent character in the sequence must be a valid non-first character (letter, digit, $, _). An identifier can be used to name a package, a class, an interface, a method, a variable, etc. An identifier may contain letters and digits from the entire Unicode character set, which supports most writing scripts in use in the world today, including the large sets for Chinese, Japanese, and Korean. This allows programmers to use identifiers in programs written in their native languages [1].
Identifier Expected Error: What It Is & What Triggers It
The initial phase of the Java compilation process involves lexical analysis of the source code. The compiler reads the input code as a stream of characters and categorizes them into lexemes of tokens, before proceeding to parse the tokens into a syntax tree. Here is where all tokens, including identifiers, are being checked against a predefined set of grammar rules. When the compiler reaches a point where, according to these rules, an identifier is expected to appear but something else is found instead, it raises the <identifier> expected error, where the angle brackets denote a reference to a token object [2].
The <identifier> expected error is a very common Java compile-time error faced by novice programmers and people starting to learn the language. This error typically occurs when an expression statement (as defined in [3]) is written outside of a constructor, method, or an instance initialization block. Another common scenario for this error is when a method parameter does not have its data type, or similarly, its name declared.
Identifier Expected Error Examples
Misplaced expression statements
When isolated expression statements such as assignments or method invocations appear outside the scope of a constructor, a method, or an instance initialization block, the <identifier> expected error is raised (Fig. 1(a)). Moving the statements in question to an appropriate place resolves this error (Fig. 1(b)).
Figure 1: <Identifier> expected on misplaced expression statement (a) errors and (b) resolution
Misplaced declaration statements
One interesting but not so obvious example of where the <identifier> expected error might appear is the try-with-resources statement [4]. This statement requires any closeable resource (such as a BufferedReader instance) to be declared within parentheses immediately after the try keyword, so it can be closed and finalized automatically. Declaring a resource variable outside the try-with-resources statement will raise the <identifier> expected error, as shown in Fig 2.
Figure 2: <Identifier> expected on misplaced declaration statement (a) error and (b) resolution
Missing method parameter data type or name
A method parameter should consist of a data type, followed by it’s name, which is an identifier. Being a statically typed language with strict grammar rules, Java treats these as crucial pieces of information—omitting either one will inevitably raise the <identifier> expected error.
In the toAbsoluteValue method in Fig. 3(a), the type of the parameter is double , but no identifier follows, only a right parenthesis. Therefore, the <identifier> expected error is raised at the position of the right parenthesis. In Fig. 3(b) the compiler assumes the parameter type to be x , but it sees no identifier next to it, hence halting with the same error.
Figure 3: <Identifier> expected on missing method parameter (a) data type, (b) name, (c) resolved by specifying both the data type and the name of the parameter
Summary
Identifiers are used to name structural units of code in Java. A compile-time error associated with identifiers and common amongst Java newcomers is the <identifier> expected error. When the Java compiler expects to find an identifier but discovers something else in its place, the compilation process fails by triggering the <identifier> expected error. With the aim to learn how to comprehend, resolve, and prevent this error, relevant examples have been presented in this article.
Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!
References
[1] Oracle, 2021. The Java® Language Specification. Chapter 3. Lexical Structure. Oracle and/or its affiliates. [Online]. Available: https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.8 . [Accessed Nov. 15, 2021].
[2] A. Reis, Compiler Construction Using Java, JavaCC, and Yacc. Hoboken, New Jersey: John Wiley & Sons, 2012, pp. 355-358.
How to Fix Java Error – Identifier Expected

—>
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 learning Java. I will explain what is the meaning of this error and how to fix it.
Table of Contents
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:
If you try to compile this class, using the javac command in the terminal, you will see the following error:
Output:
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:
The code above will compile without any issue.
Another example
Here is another example that will return the identifier expected error:
Output:
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:
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.