The following is an analysis of the difference between public class and class in Java, the need for friends can come to reference the next
You can define a class in two ways when writing a class:
public class definition class:
Class Definition classes:
if a class declaration is declared using public class, the class name must be exactly the same as the file name.
Example: Defining a Class (filename called: Hello.java)
Copy Code code as follows:
public class hellodemo{//Declare a class, a naming convention for class names: capitalize the first letter of all words
public static void Main (String args[]) {//Main method
System.out.println ("HelloWorld!!!"); System output, print
on screen
}
};
This class uses the public class declaration, the class name is Hellodemo, but the file name is Hello.java, so the following issues occur at compile time :
Copy Code code as follows:
Hello.java:1 class Hellodemo is public and should be declared in a Hellodemo.java file named
public class hellodemo{//Declare a class, the naming specification for class names: capitalize all words in the first letter
1. Error
The above error hint indicates that because the public class declaration is used, the class name should be exactly the same as the file name, that is, "Hellodemo.java" should be used to represent the name of the class.
If the declaration of a class uses class, the class name can be inconsistent with the file name, but execution is definitely performed with the generated name.
Example: The following code (the filename is called: Hello.java)
Copy Code code as follows:
class hellodemo{
public static void Main (String args[]) {
System.out.println ("HelloWorld!!!");
}
};
The filename is called Hello.java, the file name is inconsistent with the class name, but because the class declaration is used, the compilation does not produce any errors at this time, but the name of the *.class file after the build is exactly the same as the class name declared: Hellodemo.class
Execution can no longer execute Java Hello, but should execute Javahellodemo
In a *.java file, there can be only one declaration of public class, but a declaration that allows more than one class
Copy Code code as follows:
public class hello{
public static void Main (String args[]) {
SYSTEM.OUT.PRINTLN ("HelloWorld!!!");
}
};
class a{};
class b{};
In the above file, three classes are defined, and three *.class files are formed when the program is compiled.