When writing a class, you can define the class in two ways:
Public class definition class:
Class Definition class:
If a class is declared using public class, the class name must be exactly the same as the file name.
Example: define a class (File Name: Hello. java)
Public class HelloDemo {// declare a class. Naming Conventions for class names: uppercase letters of all words
Public static void main (String args []) {// main method
System. out. println ("Hello World !!! "); // System output, printed on the screen
}
};
This class uses the public class declaration. The class name is Hello Demo, but the file name is Hello. java. Therefore, the following problem occurs during compilation:
Hello. java: Class 1 HelloDemo is public and should be declared in the file HelloDemo. java
Public class HelloDemo {// declare a class. Naming Conventions for class names: uppercase letters of all words
1. Error
The above error message indicates that the class name should be exactly the same as the file name because the public class declaration is used, that is, the class name should be represented by "HelloDemo. java.
If the class Declaration uses a class, the class name may be different from the file name, but the generated name must be executed during execution.
Example: The following code is available (the file name is Hello. java)
Class HelloDemo {
Public static void main (String args []) {
System. out. println ("Hello World !!! ");
}
};
The file name is Hello. java, the file name is inconsistent with the class name, but because the class declaration is used, compilation will not produce any errors, but after *. the class file name is exactly the same as the class name declared by the class: HelloDemo. class
Java Hello cannot be executed, but java HelloDemo must be executed.
In a *. java file, only one public class can be declared, but multiple classes can be declared.
Public class Hello {
Public static void main (String args []) {
System. out. println ("Hello World !!! ");
}
};
Class {};
Class B {};
In the above file, three classes are defined, so after the program is compiled, three *. class files are formed.