Java Main Method Full Solution

Source: Internet
Author: User

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

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.