Discussion on public classes in Java learning

 

Table of contents

1. In a source file, must there be a public class?

1. Explore

2. Summary

2. Can a java source file have at most one public class?

3. Java applications must have a main class?

4. Must the main class of the application in Java be a public class?


 

1. In a source file, must there be a public class?

1. Explore

There must be a public in a source file. This is difficult for me. I really didn’t know it when I was doing it, but I only need to delete the program public above me to test it.

  Test Results

I considered that the order of the class itself might have an impact, so I swapped it

There were no errors reported twice, and the maximum value could be found normally. Therefore, there can be no public classes in a source file.

  So what exactly is public, and what is it useful for? The position I have learned so far (maybe my progress is a bit slow) has not covered this issue. But this problem will also be solved with the learning process. Searched a bit: public is the java access modifier, and there are three access modifiers (protect, frendly, private) (public, protected, friendly, private). I remembered that when I was writing a big homework in c language last semester, in order to allow the main function of c language to call the function of the cpp source file, I had to add extern (external) before each function in the .h header file of cpp. It also opened the permissions, of course I still have doubts about this.

Back to the topic, the practice of the third question proves that public can be used, and the file name can be the same as any class name. When there is public, it must be consistent with the public class. So is the location where the program starts to execute (or the main class of the program) determined by the file name? Or is it determined by the location of the main method?

  I changed the file name of the source code of the third question to Max

An error was reported, the main method was not found in the class Max. This means that the file name should be consistent with the class name containing the main method (this is wrong here, I didn't know it at the time, and it will be overturned later). Note that the compilation will not report an error, but the interpretation will report an error.

  At this time, a new problem appeared. The information I found on the Internet said that when a java source file has a public class, the file name is consistent with the public class, and there can only be one public class at most. Then I changed the class Max to public and the class input without modifiers (it is said on the Internet that if the data members and methods of a class do not have any permission modifiers, then its default access permission is friendly, here input has public modifiers main method, what kind of access is the class input?)

 The result is the same whether you add public or not, so is the statement on the Internet wrong? I changed the filename back to input but kept Max public and input unqualified.

  An error was reported during compilation. The class Max is public and should be declared in a file named Max.java.

  Explain that the statement on the Internet is not wrong.

  And why don't you report an error when you don't add public? Is it originally error-free or is it automatically set to public access when no modifiers are added?

  Since I don't know how to know what level of access permission the class is without modifiers, I can't answer this question for the time being.

  When our source file has multiple classes, I found that after javac, there will be a corresponding number of class suffix files (bytecode files)

  The name of the bytecode file is only related to the class name. When the first two classes are not public, there is an error in explaining the operation. Could it be my operation error?

  Because the java interpreter interprets the bytecode file instead of the source file, will it not report an error if I type java input?

  correct

  What if the file name is not a class name?

There is no error in the same way, which is more confusing. What I see on the Internet is that it can be consistent with any class name, but in fact it can be any name

   I add public in front of the class input, and the file name is still test.java. It has actually been verified before, and an error will be reported at this time.

2. Summary

  A source file can have no public class. When there is no public class, the file name can be consistent with any class name, and there is no limit. You can name it arbitrarily. Program execution starts from the main method. If you do not execute it from the main method, an error will be reported, saying that this class has no main method

  When the source file has a public class, there can only be one public class, and the file name should be consistent with the public class name, and the class must not contain a main method.

  When there is a public class, there may be no main method in this class. The example is Example 2 in the first chapter of the textbook (here there is a problem with the picture upload of Xuetong, I directly use the URL of the picture bed

<img src="https://pic.imgdb.cn/item/622beb745baa1a80ab4b1a5e.png">

  The program is problem-free

<img src="https://pic.imgdb.cn/item/622bec965baa1a80ab4b81f5.png">

    2. Can a java source file have at most one public class?

  

Two public, javac error: class Max should be declared in a file named Max.java, if you change the name to this, you can think of class input will report an error

3. Java applications must have a main class?

  I searched the Internet for what the main Java class is, but I saw different answers. Some said it was the class where the main method is located, and some said it was a public class defined in the program, which contained the main function or the entry point for program execution. The second statement can be seen from the above test is wrong, a java source file can not be public.

  So is the first statement correct? First of all, I didn't find the concept of the main class, so I couldn't start with the concept. But from a literal understanding, it should be the first class to be executed like the main function in the C language.

  In this case, it is conceivable that when we are working on a project with multiple source files, only one source file can have a main class, and other classes are called in the main class. Then other source files do not have a main class.

4. Must the main class of the application in Java be a public class?

In the previous example, there are many programs whose main class is not public class, for example, none of them are public class, and textbook example 2 puts the main method in class

A instead of public class people. Therefore the sixth question is wrong

  For the fifth question, first start with the code of the seventh question, which happens to be a program without a main method

<img src="https://pic.imgdb.cn/item/622becdd5baa1a80ab4b9da2.png">

    The program will report an error when interpreting and running, and there is no main method in class Dog.

  There are comments like this online:

  Any Java Application must have one and only one main method, which is the entry method of the entire program; any Applet applet requires that there must be a class in the program that is a subclass of the system class Applet, that is, the head part of the class End with extends Applet.

The main class of the application does not need to be modified with public when there is only one class in the source file, but when there are more than one class, the main class must be modified with public. The main class of the applet needs to be decorated with public at all times.

Original link: What is a java program made of? Must there be a public class in a program? What are the naming rules for java source files? _java language multiple choice questions (3)..._I have been dealing with my blog for a long time-CSDN Blog

  In fact, I saw a lot of assertions on the Internet that the main class must be modified with public, but my test proved that the main class of the java application may not be a public class.

  The practice test can only prove the error but not the correctness. I can't prove the error of the fifth question at present. There is no way of knowing whether it is correct.

  Another point is that a java application can have multiple main classes, and a java source file can also have multiple main classes. A class can only have one main, but there can be multiple main classes.

​<img src="https://pic.imgdb.cn/item/622becef5baa1a80ab4ba52b.png">

  At this time, we can choose the entrance by ourselves.

  The final answer is 2. False 3. False 4. True 5. Not sure (the answer in the book is wrong, I can’t prove it wrong) 6. False

  Summarize the final conclusion: the main class should indeed be a class with a main method. When a class has a main method, it can be used as an entry. (In fact, it doesn't matter what the main class is, we only need to know that the main method can be used as the entry point of a java program). The main class can not be modified by public, and the main class has no direct connection with the file name. A java application can have multiple main classes. As for whether a java application must have a main class, I have not yet determined

public is an access modifier, the main class is not necessarily related to public, and it is not necessary to add public before the main class. (Maybe in the future, we will make multiple source files in order to allow the JVM to easily access the main class, we set it as public) The main method is bound to public static void, which is a grammatical rule. When a source file contains public classes, the file name must be consistent with it. A java source file can have no public modified class

The main method has a fixed format public static void main, and the class containing the main method can be called the main class, which can be used as the entry point of the java application program. When we use the java class name, it must be the class containing the main method. A class can have only one main method, but there can be multiple classes with main methods. The file name has nothing to do with whether the class contains the main method

The file name must be consistent with the class name when the file contains public-modified classes. It can be any name when it does not contain public-modified classes (generally, it is not recommended to choose any name. In principle, take the name of any class, and the main class is the main class. class name)

  

  

 

Guess you like

Origin blog.csdn.net/m0_64198455/article/details/128444082