Core Java

Public Class Must Be Declared in a Matching File – Java Error Resolved

Java enforces a strict naming convention when dealing with public classes. If a class is declared public, its filename must match the class name exactly, including capitalization. Otherwise, a compilation error will occur. This error is a common pitfall for beginners and can lead to frustration when compiling Java programs. Fortunately, the fix is simple, and understanding why it happens will help you avoid it in the future. Let us delve into understanding the Java error: class X is public, should be declared in a file named X.java.

1. Introduction to the Problem

When you declare a Java class as public, the compiler expects the filename to match the class name. If it doesn’t, you’ll encounter an error like:

1
error: class X is public, should be declared in a file named X.java

Understanding this issue is crucial because mismatched filenames can lead to build failures, especially in large projects where multiple developers are working on different files.

2. Understanding the Error

Let’s consider an example that triggers this compile-time error.

2.1 Incorrect Example

Suppose we have a file named Example.java containing the following code:

1
2
3
4
5
public class Test {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

When we try to compile it using:

1
javac Example.java

We get the error:

1
Example.java:1: error: class Test is public, should be declared in a file named Test.java

2.1.1 Why Does This Happen?

In Java, the filename and public class name must match due to the following reasons:

  • Java allows multiple classes in a single file, but only one can be declared public.
  • The Java compiler uses the filename to determine which class to execute when running the program.
  • If a file contains a public class, the filename must match the class name exactly to prevent ambiguity.

3. How to Fix It

The solution is straightforward: the filename must match the public class name.

3.1 Solution 1: Rename the File

Rename the file to Test.java so that it matches the public class name:

1
2
3
4
5
6
// File: Test.java
public class Test {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

Now compile the file with:

1
2
javac Test.java
java Test

Once executed, the following output is returned:

1
Hello, World!

3.2 Solution 2: Remove the public Modifier

If you want to keep the filename as Example.java, another solution is to remove the public modifier from the class:

1
2
3
4
5
6
// File: Example.java
class Test {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

Since the class is no longer public, Java allows it to be in a differently named file.

3.3 Best Practice

To avoid confusion, always ensure that the filename matches the public class name. This makes your code more readable and prevents unnecessary compilation errors.

4. Conclusion

This error occurs because Java enforces strict filenames and public class name matching. The two possible fixes are:

  • Renaming the file to match the public class name.
  • Removing the public modifier from the class.

By following these rules, you can avoid this compile-time error and write Java programs that compile successfully. Always ensure that your filenames and class names align to maintain clarity and consistency in your codebase.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button