The idea of Java programming that you don't know

Source: Internet
Author: User

Read thinking in Java This book, there is such a phrase "inside a compilation unit, you can have a public class, it must have the same name as the file"

Have the following questions:

    1. In a class that says you can have a public class, is that what it means?

The following experiment was carried out with this problem: (filename is Test1.java)

classTest1 { Public Static voidMain (string[] args) {if(Args! =NULL&& args.length > 0) {System.out.println (args.length);  for(intI=0;i < args.length;i++) {System.out.println ("args[" +i+ "]:" +Args[i]); }           }       }        Public Static voidTest () {}}classtest3{ Public Static voidMain (string[] args) {if(Args! =NULL&& args.length > 0) {System.out.println (args.length);  for(intI=0;i < args.length;i++) {System.out.println ("args[" +i+ "]:" +Args[i]); }           }       }        Public Static voidTest () {}}

Execute the command under the terminal without any errors, it can be seen that the first one is really feasible, and there is really no need for a public to specify the relevant class name in a class file.

Then the second question is, since there is no public modification, it is not inside the class name can also be arbitrary, and then modify the above code in TEST1 to Test2 and test, the result is as follows:

The same results are almost the same, so it can be concluded that there is no public-decorated class in a class file, and the class name does not have to be the same as the file name.

Through the above research can be clearly seen in the "Thinking in Java" in a mistake, the exact words are as follows:

For example, assume that the file name is Myclass.java. It means that there is one and only one public class in that file. And the name of that class must be MyClass (including case) "

--taken from 5.1 knots

Extended Understanding:

The following is a list of the class name modifiers (taken from Baidu)

     public Common class default    class (default modifier, only objects in the same package can use such a class)   abstract class final      cannot be inherited class static     static class private    can only be decorated with internal classes, generally not recommended

  

The idea of Java programming that you don't know

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.