0
Follow
0
View

Who knows the basics of java?

cynthia_5199 注册会员
2023-02-28 06:49

In java, the main() method is the entry method of a java application. That is, the main() method is the first method executed when the program is running. This method is quite different from other methods, such as the method name must be main, the method must be of type public static void, the method must be an argument that takes an array of strings, and so on.
Because the main() method is called by the Java virtual machine, it must be public. When the virtual machine calls the main() method, it does not need to produce any objects, so the main() method is declared static and does not need to return a value, so it must be declared void!
It is not necessary that the main class of a java application be public. It is possible to run

without public
dingyungao1 注册会员
2023-02-28 06:49

In Java, an application must have a main class, which is the class that contains the main() method. The main() method is the entry point of a Java program, where the program begins execution.

The main class of a Java application need not be a public class. Although the main class used in many of the sample code is the public class, this is not actually required. The main class of a Java application can be any class, as long as it contains a public static void main(String[] args) method. For example, the following code is a complete Java application whose main class is not public:

class MyMainClass {

    public static void main(String[] args) {

        System.out.println("Hello, world!");

    }
}

Note that if the main class is not public, you need to pay attention to the visibility of the class when compiling and executing. If the main class is in the same package as other classes, you can use the main class directly; If the main class is not in the same package as the other classes, you need to import the main class using import statements in the other classes.

duanchigui 注册会员
2023-02-28 06:49

In Java, applications need to have a Class containing the main() method as an entry point, so it is common to specify a Main Class to run the application. But not all Java applications need to have master classes, such as lightweight utility classes, library classes, and so on.

It is not necessarily true that the main class must be public. If the Java source file of the main class contains only the definition of that class, and the file name is the same as the name of the main class, then the main class can be of type public or non-public. But if more than one class is defined in the Java source file, then the main class must be defined as public and the file name must be the same as the main class name for it to be properly recognized by the Java compiler.

About the Author

Question Info

Publish Time
2023-02-28 06:49
Update Time
2023-02-28 06:49