1. Overloading of main method
package cn.nxl; public class Main_test { public static void main(String args[]) { System.out.println("main method of application entry"); main(); main(); main(, ); } public static void main(){ System.out.println("main method without parameters"); } public static void main(int i){ System.out.println("overload main method with parameters"); } public static void main(int i,int j){ System.out.println("overload main method with two parameters"); } }The above example shows that the main method can be overloaded. The parameters of each main method are different. The program can run and can output the desired results. However, if there is no main method for the program entry, only other main methods are included. Although it is possible to compile this program, an error will be generated during runtime. This is because when the program is running, the Java virtual machine cannot find the corresponding main method, which will generate a running error.
2. Calling of main method
public class Main_test { public static void main(String[] args) { main(args); } public static void main(String[] args){ main(args); } }After running the program, the program executes two main methods infinitely recursively. It is obvious that the main method can be called.
3. Inheritance of main method
Main.java: public class Main{ public static void main(String[] args) { System.out.println("Hello Word!"); } } Main_test.java: public class Main_test extends Main{ }After compiling and running Main_test.java, "Hello Word!" is output, indicating that the main method can be inherited.
4. Hiding of main method
Main.java: public class Main{ public static void main(String[] args) { System.out.println("Main"); } } Main_test.java public class Main_test extends Main{ public static void main(String[] args) { System.out.println("Main_test"); } }It is obvious that the main method in the parent class Main is hidden, and the result shows the content of the main method in the subclass.
Summarize
The above is the Java main method introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!