Java compilation error:
Error: Class Something is public and should be declared in a file named Something.java
As shown in the following:
The first explanation:
"Class A is public and should be declared in a file named A.java" This sentence needs to be understood in two steps :
1. In Java, if Class A is declared public, then class A must be stored in a file named A.java;
2, conversely, in a Xxx.java file contains a maximum of a top-level public class, and the public class name is the same as the file name. For example, in file A.java, one or more classes are allowed to be defined, but at most a top-level public class is allowed, and this class is named A.
Note: the top-level emphasis here is to allow non-top-level public classes to exist, such as internal public classes (that is, public classes in inner classes).
The second explanation:
This is your naming error, change the file name to A.java because the class in the file is public class A {},
If you can get rid of this public, then the file name can be arbitrary. Otherwise the file name will be consistent with the class name.
The third explanation:
There can be at most one class in a file that is public, so you can expose the definition of a class to be accessible to other classes! If you already have this public class, the file name must be the same as the class name!
Also note: if only one. java file is built, public can only declare before public static void main (string[] args) {}, Otherwise, even if the class name of the defined public is the same as the name of the Java file you declared, an error will be compiled.
Error: Class Something is public and should be declared in a file named Something.java