Home >Java >javaTutorial >Why Does My Java Compiler Show 'Error: class X is public, should be declared in a file named X.java'?
When attempting to compile a Java program, a compiler error is encountered: "Error: class X is public, should be declared in a file named X.java."
This error occurs when the public class within a .java file does not match the file's name.
To resolve this issue, there are two options:
Option 1: Rename the Java file to match the public class's name.
Option 2: Rename the public class to match the file's name.
Here is the corrected code:
// WeatherArray.java public class WeatherArray { public static void main(String[] args) { // ... } }
Alternatively:
// Main.java public class Main { public static void main(String[] args) { // ... } }
The above is the detailed content of Why Does My Java Compiler Show 'Error: class X is public, should be declared in a file named X.java'?. For more information, please follow other related articles on the PHP Chinese website!