1. Overloading of the Main method
1 Packagecn.nxl2018;2 3 Public classMain_test {4 Public Static voidMain (String args[]) {5System.out.println ("Main method of application entry");6 main ();7Main (10);8Main (10, 10);9 }Ten Public Static voidMain () { OneSystem.out.println ("Main method without parameters"); A } - Public Static voidMaininti) { -SYSTEM.OUT.PRINTLN ("Overloaded Main method with arguments"); the } - Public Static voidMainintIintj) { -SYSTEM.OUT.PRINTLN ("Overloaded Main method with two arguments"); - } +}The above example shows that the main method can be overloaded, the parameters of each main method are different, the program can be run, you can output the expected results. However, if there is no program entry for the main method, only the other main method is included. Although compiling this program is possible, the runtime generates an error. This is because when the program runs, the Java Virtual machine cannot find the corresponding main method, which results in a run-time error.
2. Invocation of the Main method
1 Public classMain_test {2 Public Static voidMain (string[] args) {3 main2 (args);4 }5 Public Static voidmain2 (string[] args) {6 main (args);7 }8}after running the program, the program infinitely recursively executes two main methods, it is obvious that the main method can be called.
3. Inheritance of Main method
Main.java:
1 Public class main{2 Public Static void Main (string[] args) {3 System.out.println ("Hello word!" ); 4 }5 }
Main_test.java:
1 Public class extends main{2 }
after compiling the run Main_test.java, the "Hello word!" is output, stating that the main method can be inherited. 4, the main method of hiding
Main.java:
1 Public class main{2 Public Static void Main (string[] args) {3 System.out.println ("Main"); 4 }5 }
Main_test.java
1 Public class extends main{2 Public Static void Main (string[] args) {3 System.out.println ("Main_test"); 4 }5 }
It is obvious that the main method in the parent class is hidden, and the result shows the contents of the main method in the subclass. Affiliate Blog (blog Park):
Java Main Method Full Solution