HOWTO · Java
How to Fix the `<identifier> expected` Error in Java
Learn what javac means by `<identifier> expected` and fix missing parameter names, misplaced statements, and invalid try-with-resources syntax.
On this page
The Java compiler reports <identifier> expected when it reaches a token where Java syntax requires a name, such as a variable, method, or class name. Look at the line marked by javac, then inspect the declaration or statement immediately before the caret. The error is often caused by a missing parameter type or name, an executable statement in a class body, or an invalid resource declaration.
Understand the <identifier> expected diagnostic
An identifier names a Java program element. Java identifiers may contain letters, digits, $, and _, but they cannot begin with a digit and cannot be a reserved keyword. A parameter declaration needs both a type and an identifier, for example int count. A class body may contain field declarations and initializer blocks, but an assignment or method call cannot appear there as a free-standing statement.
javac reports the location where its grammar can no longer continue; that location is not always the root cause. Check missing braces, semicolons, commas, and parentheses before changing the marked token. Compile again after fixing the first diagnostic because later messages can be consequences of the same syntax error.
Fix a missing parameter type or name
Every method parameter needs a data type followed by a variable name. This invalid declaration omits the type:
public class Demo {
static int square(x) {
return x * x;
}
}
Compiling this invalid form with javac produces a diagnostic like this (the
line number and caret can vary by JDK):
Demo.java:2: error: <identifier> expected
static int square(x) {
^
1 error
The corrected declaration supplies int before x:
public class Demo {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
System.out.println(square(10));
}
}
Compile it with javac Demo.java and run java Demo; the output is:
100
The reverse mistake also fails: static int square(int) has a type but no parameter identifier. Add a name and use that same name in the method body. Do not use a keyword such as class or return as the name; choose a valid identifier such as value.
For example, static int square(int) reports the same diagnostic because
javac expects a parameter name:
Demo.java:2: error: <identifier> expected
static int square(int) {
^
1 error
Move executable statements into a method or initializer
Fields can be declared directly in a class, but assignments and method calls must be inside a method, constructor, or initializer block. This class-body code is invalid:
public class Demo {
private String name;
name = "Naruto";
System.out.println(name);
}
The compiler reports the invalid assignment and method call at the tokens where
the class-body grammar expects a declaration or block:
Demo.java:3: error: <identifier> expected
name = "Naruto";
^
Demo.java:4: error: <identifier> expected
System.out.println(name);
^
3 errors
Put the executable statements in a method and call it from main:
public class Demo {
private String name;
void print() {
name = "Naruto";
System.out.println(name);
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.print();
}
}
The successful output is:
Naruto
If the assignment is intended to initialize a field, an instance initializer ({ name = "Naruto"; }) is another valid option. A field initializer may also use an expression, such as private String name = "Naruto";, but it still cannot contain a stand-alone method call outside an initializer or method.
Declare resources correctly in try-with-resources
The resource specification accepts a resource declaration, such as BufferedReader reader = ..., or a reference to an already initialized final or effectively final variable. Assigning a new resource to a variable in the resource specification is not the declaration form:
import java.io.BufferedReader;
import java.io.StringReader;
public class Demo {
public static void main(String[] args) throws Exception {
BufferedReader reader = null;
try (reader = new BufferedReader(new StringReader("Java"))) {
System.out.println(reader.readLine());
}
}
}
On the tested JDK, assigning to the already declared reader is rejected in
the resource specification:
Demo.java:7: error: the try-with-resources resource must either be a variable declaration or an expression denoting a reference to a final or effectively final variable
try (reader = new BufferedReader(new StringReader("Java"))) {
^
1 error
Declare the resource inside try instead:
import java.io.BufferedReader;
import java.io.StringReader;
public class Demo {
public static void main(String[] args) throws Exception {
try (BufferedReader reader = new BufferedReader(new StringReader("Java"))) {
System.out.println(reader.readLine());
}
}
}
It prints:
Java
The corrected form closes reader automatically at the end of the try block. If code needs the reader afterward, copy the required value while the resource is open; do not use the closed resource after the block.
Checklist before recompiling
When <identifier> expected remains, check these boundaries in order:
- Confirm each parameter has both a type and a name.
- Check that the proposed name is not a keyword, does not start with a digit, and uses the intended case.
- Move assignments, method calls, and other executable statements into a method, constructor, or initializer.
- Inspect the braces and punctuation on the preceding lines.
- For try-with-resources, use a declaration or a previously initialized effectively final resource.
These rules are stable Java syntax and apply to current JDK releases, including Java SE 27. The compiler version can change the exact wording or line number of a diagnostic, so treat the message and caret as a clue to the surrounding grammar rather than as a guarantee that the caret marks the original typo.