Java detailed analysis of class and public class differences

Source: Internet
Author: User

The difference between public class and class
There are two ways to define a class:

  • public class class name
  • Class name

I can remove the public from the front of class, and if you declare class with the public class, then the file name must match the class name, which is the rule, what if the file name and class name are inconsistent? Let's change the file name of the previous Helloworld.java, for example, I changed to World.java, and then execute the javac command, you can see the following error message:

If a class is used to declare classes, the file name can be any legal filename, the filename does not need to be the same as the class class, and I remove the public from the code. The file name is still World.java, recompile with the Javac command, you can see that a new Helloworld.class file is generated, and then run Java HelloWorld, and you can see that the program is still working. One conclusion here is that the name of the class file compiled with the Javac command is not related to the Java filename, but is consistent with the class name.

In a Java file, you can declare multiple classes, but only one public class is declared, and the sample code:

After compiling with the Javac command, you will find that each class will generate a corresponding. class file

Java detailed analysis of class and public class differences

Related Article

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.