Java programs are built from blocks inside blocks: a class, methods inside it, ifs and loops inside those. Every block starts with { and ends with }. Lose one brace, or add one too many, and the compiler gives you messages that seem to have nothing to do with braces, and often point at the wrong line.
This guide covers the brace errors beginners meet most, what each one means and how to find the brace that's really at fault. We compiled every example with Java 17, 21 and 25, because, as you'll see, the newer versions word some of these errors very differently.
reached end of file while parsing
public class Main {
public static void main(String[] args) {
int hours = 3;
if (hours > 2) {
System.out.println("That's a lot of screen time.");
System.out.println("Done.");
}
}Main.java:9: error: reached end of file while parsing
}
^
1 errorWhat it means: the compiler read the whole file and got to the end while a block was still open. Parsing is the compiler reading your code to work out its structure, and it ran out of file before the structure was complete. In other words: there's at least one { without its }.
Why it points at the wrong place: the missing brace belongs after line 5, to close the if. But the compiler can't know that. It happily reads the } on line 8 as the end of the if, the one on line 9 as the end of main, and then the file ends while the class is still open. So it reports the very last line, the only place where it's sure something is missing.
The fix: find the block that was never closed and add its } where it belongs:
if (hours > 2) {
System.out.println("That's a lot of screen time.");
}
System.out.println("Done.");This message is the same in Java 17, 21 and 25.
class, interface, enum, or record expected
The opposite problem: one } too many. Here, an extra brace closes the class before the second method:
public class Main {
public static void main(String[] args) {
System.out.println(average(7, 2));
}
}
static double average(int total, int count) {
return (double) total / count;
}
}The } on line 5 ends the class Main. Everything after it, the method average and the last }, is now outside any class, and that's where this message comes from. But what exactly the compiler says depends on your Java version.
Java 17 says it plainly, once for the method and once for the stray brace:
Main.java:7: error: class, interface, enum, or record expected
static double average(int total, int count) {
^
Main.java:9: error: class, interface, enum, or record expected
}
^
2 errorsThat is: at the top level of a file, outside any class, Java only expects the start of a new class, interface, enum or record. A method isn't allowed there. (Older versions from before records were added, like Java 8 and 11, say class, interface, or enum expected.)
Java 21 adds a confusing first line:
Main.java:7: error: unnamed classes are a preview feature and are disabled by default.
static double average(int total, int count) {
^
(use --enable-preview to enable unnamed classes)
Main.java:10: error: class, interface, enum, or record expected
}
^
2 errorsJava 25 says this:
Main.java:10: error: class, interface, annotation type, enum, record, method or field expected
}
^
1 errorWhy the newer versions talk about methods and "unnamed classes": since Java 25, a small program doesn't need a class line at all: a file can contain just methods, starting with void main(). (Java 21 had a preview of the same idea, under the name "unnamed classes".) So when the compiler finds a method outside your class, newer versions no longer say "that's not allowed"; they assume you might be writing one of these class-less programs. For a beginner, the advice is the same in every version: a } too early has closed your class.
The fix: remove the extra } on line 5, so that average is inside the class again.
A method below the class
A related mistake: you add a new method after the class has already ended, below its final }:
public class Main {
public static void main(String[] args) {
System.out.println("Hi");
}
}
static void greet() {
System.out.println("Hello");
}Here the three versions disagree completely:
Main.java:7: error: class, interface, enum, or record expected
static void greet() {
^Main.java:7: error: unnamed classes are a preview feature and are disabled by default.
static void greet() {
^
(use --enable-preview to enable unnamed classes)Main.java:1: error: compact source file does not have main method in the form of void main() or void main(String[] args)
public class Main {
^The Java 25 message is the most puzzling one: it points at line 1 and complains about main, which looks perfectly fine. What happened is that Java 25 treated the whole file as a "compact source file", a class-less program, because of the method outside the class, and then looked for a main at the top level of the file. It didn't find one, because yours is inside the class.
The fix is the same in all three: move the method inside the class, above its last }.
Two more errors from braces in the wrong place
<identifier> expected
Code that should be inside a method, written directly in the class:
public class Main {
System.out.println("Hi");
public static void main(String[] args) {
}
}Main.java:2: error: <identifier> expected
System.out.println("Hi");
^
Main.java:2: error: illegal start of type
System.out.println("Hi");
^
2 errorsDirectly inside a class, Java only expects declarations: fields (variables of the class) and methods. A statement like System.out.println(...) must be inside a method. Move it into main. All three versions report this the same way.
illegal start of expression
A method that starts before the previous one has ended:
public class Main {
public static void main(String[] args) {
System.out.println("Hi");
static void greet() {
System.out.println("Hello");
}
}Main.java:5: error: illegal start of expression
static void greet() {
^
1 errormain is still open when greet begins, and Java doesn't allow a method inside a method. The missing } belongs at the end of main, just before the blank line. Again, the same message in all three versions.
How to find a missing or extra brace
Brace errors are easy to fix once you've found the brace, and the hard part is finding it. These habits help:
- Let the editor indent your code. Use your editor's format command: in VS Code it's Format Document (Shift+Alt+F on Windows, Shift+Option+F on a Mac, Ctrl+Shift+I on Linux), in IntelliJ IDEA Reformat Code (Ctrl+Alt+L, or Cmd+Option+L on a Mac). After formatting, every block is indented one level deeper than the one around it, and a missing or extra brace shows up as code that suddenly sits too far left or right.
- Click next to a brace. Most editors highlight the matching brace. If the
{of yourifmatches the}you meant for the method, you've found the problem. - Type the closing brace straight away. When you write
{, write the}right after it and then fill in the middle. Many editors do this for you. - Fix the first error first. One wrong brace can cause several errors further down. Fix it, compile again, and many of the others disappear. Our guide How to read an error message explains why.
A quick checklist
reached end of file while parsing: a{is missing its}. Look for an unclosedif, loop or method, usually somewhere above the line in the message.class, interface, enum, or record expected(and its Java 21 and 25 versions about unnamed classes, methods or fields): a}closed the class too early, or a method sits below the class.compact source file does not have main methodin Java 25: almost always a method outside your class, not a problem withmainitself.<identifier> expectedon a statement: it's in the class but outside any method.illegal start of expressionon a method: the method before it isn't closed.
Once your braces are right, the next error most beginners meet is about types: incompatible types: String cannot be converted to int. And when the program compiles but crashes while running, see Java NullPointerException.
