Java's main function

Source: Internet
Author: User

1. The standard main Function form

For the main function, as long as it is

 Public Static void Main (string[] args)  Public Static void Main (String ... args)  Public Static void Main (String args[])

In this way, the virtual machine can find and enter the program, if your main function does not write this, you will be prompted to find the main function.

Modification of 2.main functions

(1) public because it needs to be called from outside;

(2) static, the class where main is not yet instantiated;

(3) Void, indicating that no value is returned.

General of 3.main functions

Except that main is the entrance to the program, the main function is a normal function, and each class can have one. We can make a call to the main function, overload ...

4. Overloading the Main method

 Public classTest { Public Static voidMaininti) {System.out.println ("Int main:" +i); }     Public voidMainCharc) {System.out.println ("Char main:" +c); }     Public voidMain (String s) {System.out.println ("Char main:" +s); }     Public Static voidMain (string[] args) {Test temp=Newtest (); Main (11); Temp.main (' t '); Temp.main ("Temp"); }}

5. Call the Main method

 Public classTest {Private Static BooleanDone =false;  Public Static voidprint () {if(!Done ) { Done=true; System.out.println ("In print"); String[] args= {"A", "B", "C"};        Main (args); }    }         Public Static voidMain (string[] args) { for(String S:args) System.out.print (s+ " ");    Print (); }}

6. Inherit the Main method

 class  " Span style= "color: #000000;" > SuperTest { public  static  void   main (string[] args) {System.out.println ( "in Super" ); }}  public  class  test extends   supertest{ public  static  void   main (string[] args) {Supert        Est.main (args);    SYSTEM.OUT.PRINTLN ( in test "); }}

7. Call the main method of another program

Here I just call the same. java file under the class, you can call different. Java classes, different package classes of the Main method, as long as you can give the correct path in Class.forName ().

 Packagetest;ImportJava.lang.reflect.Method; Public classtestclass{ Public Static voidMain (string[] args) {Class<another> C =NULL; Try{C= (class<another>) class.forname ("Test.another");//find a class for another programMethod m = C.getdeclaredmethod ("main", Args.getclass ());//find the main method of the classM.invoke (NULL, (Object)NewString[0]); } Catch(Exception e) {e.printstacktrace (); } System.out.println ("In Test"); }}classanother { Public Static voidMain (string[] args) {System.out.println ("In another"); }}

Java's main function

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.