Differences between public class and class declaration in java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.