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{//Declaring a class, the 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, printing on the screen
}
};
This class uses the public class declaration, the class name is Hellodemo, but the file name Hello.java, so the compile-time appears as follows
question:
Copy Code code as follows:
The Hello.java:1 class Hellodemo is public, and should be declared in the name Hellodemo.java file
public class hellodemo{//Declaring 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.