The following describes the differences between public class and class in java.
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)
Copy codeThe Code is as follows:
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 ("HelloWorld !!! "); // System output, printed on the screen
}
};
This class uses the public class declaration. The class name is HelloDemo, but the file name is Hello. java. Therefore, the following occurs during compilation:Problem:
Copy codeThe Code is as follows:
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)
Copy codeThe Code is as follows:
Class HelloDemo {
Public static void main (String args []) {
System. out. println ("HelloWorld !!! ");
}
};
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.
Copy codeThe Code is as follows:
Public class Hello {
Public static void main (String args []) {
System. out. println ("HelloWorld !!! ");
}
};
Class {};
Class B {};
In the above file, three classes are defined, so after the program is compiled, three *. class files are formed.