In Java, can a single source file contain more than one class declaration, and what are the rules for public classes and file names?

Difficulty: Medium

Correct Answer: Yes, a source file can contain multiple classes, but at most one top-level class can be public, and its name must match the file name.

Explanation:


Introduction / Context:
Java developers often organize their code by placing one class per source file, but this is a convention rather than an absolute technical requirement. The Java Language Specification defines clear rules about how many classes can appear in one .java file and how public classes relate to the file name. Interviewers like this question because it tests your understanding of compilation units and visibility rules in Java projects.


Given Data / Assumptions:

  • We are working with standard Java source files with the .java extension.
  • Classes can be top-level or nested (inner) classes.
  • Public is the highest access level for top-level classes.
  • The Java compiler uses the file name and class declarations to generate .class files.


Concept / Approach:
A single Java source file may contain multiple top-level class declarations, as well as interfaces and enums. However, there is a special rule for public top-level classes: at most one of them may be public, and if a public class exists in the file, the file name must exactly match that public class name (case sensitive). Other top-level classes in the same file must use the default package access (no public, protected, or private modifier) and will be accessible only within the same package. This rule helps the compiler and build tools find public classes easily based on file names.


Step-by-Step Solution:
Step 1: Recognize that Java allows multiple top-level classes in one .java file. Step 2: Recall that only one of those top-level classes is allowed to be declared public. Step 3: Understand that if there is a public top-level class, the file name must match that class name, for example MyClass.java for public class MyClass. Step 4: Note that additional top-level classes in the same file must have default (package private) access. Step 5: Conclude that the compiler generates separate .class files for each top-level class, regardless of how many appear in the source file.


Verification / Alternative check:
You can test this by creating a file Sample.java that contains public class Sample and another class Helper with no access modifier. The code compiles and produces Sample.class and Helper.class. If you try to declare two public classes in the same file, or if you give the file a name that does not match the public class, the compiler produces an error. This confirms that Java allows multiple classes per file but limits public top-level classes to one per file whose name must match the file name.


Why Other Options Are Wrong:
Option B is incorrect because it states that only one class can be declared in a file and that it must be public, which is not true; multiple non-public classes can share a file. Option C is wrong because Java does not allow multiple public top-level classes in a single source file, and file name rules are strict for public classes. Option D is incorrect and exaggerates the restriction; Java does not require separate files per method and allows multiple classes in the same file under the described rules.


Common Pitfalls:
A common pitfall is misunderstanding error messages when the file name does not match the public class name, leading developers to believe multiple classes per file are not allowed at all. Another issue is placing many unrelated top-level classes into one file, which can harm readability and maintainability. Good practice is usually to keep one public class per file, with additional top-level classes used sparingly and only when they are closely related and package private by design.


Final Answer:
Yes, a Java source file can contain multiple classes, but at most one top-level class may be public, and that public class name must match the file name.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion