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