Can you have multiple classes in one Java file?
Codemia Knowledge Hub ·
Yes. A single .java file can declare several classes, but only one of them can be public, and that public class must match the filename. The other top-level classes must be package-private (no access modifier). The compiler still produces a separate .class file for each class.
The one public class per file rule
JLS 7.6 lets a compiler require that a public top-level class live in a file named after it, and javac enforces this, so in practice a file can hold at most one public class.
Compiling OrderProcessor.java produces three .class files:
OrderProcessor.classOrderValidator.classOrder.class
At the bytecode level each class is independent. Sharing a source file is purely a source organization choice, and since deployment tools and classloaders work with .class files, it is invisible at runtime.
The error from two public classes
Declaring a second public class in the same file fails to compile:
javac reports:
Eclipse words the same error as "The public type MyHelper must be defined in its own file".
The compiler relies on the filename to locate public classes during compilation. Without that convention it would have to scan every file to find a class definition.
Kinds of class declarations in one file
Public top-level class
One per file, visible to all packages. This is the standard case.
Package-private top-level class
Visible only within the same package. Useful for implementation details that support the public class.
Package-private does not mean private to the file. Every class in the same package can use it, so a "helper" that another class in the package starts referencing becomes an unintended coupling.
Inner class (non-static nested)
An inner class is tied to an instance of the enclosing class and can access all of its fields and methods, including private ones. It also holds an implicit reference to the enclosing object, which keeps that object from being garbage collected. Use a static nested class unless you need access to the outer instance.
Static nested class
It behaves like a regular class scoped inside another, and is common for builders, iterators and entry types (like Map.Entry).
Comparison of declaration types
| Declaration Type | Access Modifier | Visibility | Separate .class File | Can Access Outer Instance |
| Public top-level | public | All packages | Yes | N/A |
| Package-private top-level | (none) | Same package only | Yes | N/A |
| Inner class | Any | Depends on modifier | Yes (Outer$Inner.class) | Yes |
| Static nested class | Any | Depends on modifier | Yes (Outer$Nested.class) | No |
| Local class (in method) | (none) | Enclosing method only | Yes (Outer$1Local.class) | Yes (if non-static method) |
| Anonymous class | (none) | Expression only | Yes (Outer$1.class) | Yes (if non-static context) |
When multiple top-level classes make sense
Small, tightly coupled helpers
A helper that is only a few lines long and used only by the file's primary class:
Sealed class hierarchies (Java 17+)
Sealed classes often define their permitted subclasses in the same file:
Keeping the whole hierarchy in one place is natural for algebraic data types used with pattern matching.
Same-file placement is a choice, not a requirement: the permits clause can name subclasses in other files, as long as they are in the same package (or, in a named module, the same module).
Prototyping and competitive programming
One file reduces friction when you are prototyping or solving contest problems:
Why production code uses one class per file
- IDE navigation. IDEs map filenames to class names. If
OrderValidatorlives inOrderProcessor.java, "Go to Class" does not lead you to an obvious file. - Version control. Changes to
OrderValidatorshow up as diffs onOrderProcessor.java, which makes history harder to follow. - Code review. Reviewers expect a class to be in the file with its name. Hunting for package-private classes slows reviews down.
- Package structure. If many related classes end up in one file, that usually means you need a sub-package. Create it and give each class its own file.
Related reading
- Java Multiple Inheritance
- Java Multithreading for IVRS with GSM Modem rxtx playing voice file making event listener stop working
- Java Naming Convention with Acronyms
- Java NIO FileChannel versus FileOutputstream performance / usefulness
- Java Non-Blocking and Asynchronous IO with NIO NIO.2 JSR203 - Reactor/Proactor Implementations
- Java notify() vs. notifyAll() all over again
- Java Open Source Text Mining Frameworks
- Java Ordered Map
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.