The Java compiler javac is refusing to compile your code because it found a public class that isn’t in a file with a matching name, which is a fundamental rule for Java’s organization.

Here are the common reasons why this happens and how to fix them:

  • Multiple Public Classes in One File: This is the most frequent culprit. A single .java file can only contain one public class. If you have more than one, javac will flag this error.

    • Diagnosis: Open the .java file in question and look for multiple public class declarations.
    • Fix: Separate each public class into its own .java file. For example, if MyClass.java contains public class MyClass and public class AnotherClass, you need to move public class AnotherClass into a file named AnotherClass.java.
    • Why it works: Java requires each public class to reside in a file named exactly after the class, including capitalization. This naming convention is how the compiler and runtime environment locate and load classes efficiently.
  • Inner Public Class: You might have a public class declared inside another class, which is not allowed. Inner classes can be public, but only if they are non-static inner classes (often called member classes or anonymous classes). A public static class nested directly within another class is permissible, but a public class at the same level as the outer class declaration is not.

    • Diagnosis: Examine the structure of your .java file. Look for a public class declaration that is nested within another class’s curly braces {}.
    • Fix: If the inner public class is intended to be a top-level class, move its declaration out of the enclosing class and into its own .java file. If it’s meant to be an inner class, ensure it’s declared as public static class if it needs to be accessed without an instance of the outer class, or simply public class if it’s intended to be a non-static member and you’re encountering this error due to a misunderstanding of the rule. The error message specifically points to a public class declaration that should be in its own file, implying it’s not a valid nested structure.
    • Why it works: Java enforces a strict hierarchy. A public class at the top level of a file is the primary entity that file defines. Nested public classes can lead to ambiguity and conflicts in how they are referenced and loaded.
  • Incorrect File Naming: Even if you only have one public class, if the .java file is not named precisely after that public class, you’ll get this error. Case sensitivity matters!

    • Diagnosis: Compare the name of your .java file with the name of the public class declared within it.
    • Fix: Rename the .java file to match the public class name exactly. For instance, if your file is myclass.java but the public class is public class MyClass, rename the file to MyClass.java.
    • Why it works: The Java Virtual Machine (JVM) and the compiler rely on this filename-to-public-class-name mapping for class loading and compilation. It’s a core part of Java’s modularity and organization.
  • Package Declaration Mismatch (Less Common for this Specific Error): While usually leading to different errors, a misplaced package declaration or a package structure that doesn’t align with the file’s location can sometimes manifest in confusing ways. However, for this specific error message, it’s almost always about the public class declaration itself.

    • Diagnosis: Check the package statement at the top of your .java file. Ensure it correctly reflects the directory structure where the file resides.
    • Fix: Adjust the package declaration to match the directory structure. If the file is in src/com/example/, the declaration should be package com.example;.
    • Why it works: Packages provide a namespace for classes. The compiler and JVM expect a direct correspondence between the declared package and the physical location of the .java or .class file.
  • Accidental Copy-Paste: Sometimes, you might have accidentally copied and pasted a public class declaration from another file into your current one, leading to duplicate public class definitions.

    • Diagnosis: Review your file for any pasted code blocks that might contain a public class declaration that doesn’t belong there.
    • Fix: Remove the extraneous public class declaration and its associated code. If the copied class is needed, move it to its own appropriate file.
    • Why it works: As mentioned, a .java file can only define one public entity, which is its public class. Extra declarations violate this rule.
  • Using an IDE’s Incorrect Refactoring: If you’ve used an IDE’s "move class" or "rename class" feature, and it didn’t complete correctly or you interrupted the process, you might end up with a public class declaration in the wrong place or file.

    • Diagnosis: Review recent changes made through your IDE’s refactoring tools. Check the file history if available.
    • Fix: Manually correct the file structure and class declarations to ensure each public class is in a file of the same name, and that there’s only one public class per file. Sometimes, reverting the refactoring and re-applying it carefully can resolve the issue.
    • Why it works: IDEs automate these operations, but errors can occur. Manual correction ensures adherence to Java’s strict file organization rules.

After fixing these issues, the next error you might encounter, especially if you had multiple public classes that you’ve now separated, is a package declaration mismatch if the files aren’t placed in the correct directory structure corresponding to their package names.

Want structured learning?

Take the full Java course →