Transferred from: http://blog.csdn.net/qdieyou/article/details/1490618
————————————————————————————————
The full definition syntax for the main function is public static void main (string[] args)
The main method is a special method, which is the entry point for the main program access, and can be decorated with public, default (that is, nothing), protected, private, and no run-time errors occur.
Where public is not mandatory, declared public is for the main method to be called from any Java runtime environment. However, static is required so that you can run the application directly without having to construct the class instance.
The main method has no return value, and the return value declaration must be void, not defined or defined as a different type.
Example 1:
Which of the following is the correct form of the Main method?
(1) static void Main (string[] args) {}
(2) public static void main (string[] args) {}
(3) public static void Main (String args[]) {}
(4) public static void Main (String door[]) {]
Answer: 1,2,3,4
The following file "Testmain.java"
Test the writing of the main function
public class testmain{
/*
public static void Main (string[] args) {//Can be compiled, and can run
System.out.println ("Test ok!");
}
*/
/*
static public void Main (string[] args) {//Can be compiled, and can run
System.out.println ("Test ok!");
}
*/
/*
static void Main (string[] args) {//can be compiled, but not run, hint: Main method not public
System.out.println ("Test ok!");
}
*/
/*
public static void Main (String args) {//can be compiled, but not run
System.out.println ("Test ok!");
}
*/
/*
public void Main (string[] args) {//can be compiled and not run because the static modifier is required.
System.out.println ("Test ok!");
}
*/
/*
void Main (string[] args) {//can be compiled, not run
System.out.println ("Test ok!");
}
*/
public static void Main (string[] args) {
System.out.println (args.length);
System.out.println (args[2]);//input Java testmain on the command line, Args[2] does not exist, so there is an exception, not normal operation
}
}
End: The main function must be public, static, void, and the argument must be of type string, where the order can be changed and the variable name can be changed. The absence of a compilation may pass, but the run is not able to pass.
Attach your own test code
public class Test1 {
/**
* can be compiled, but cannot run
*/
public static void main () {
System.out.println ("123"); br>}
/**
* can run normally
* @param string
*/
/*public static void Main (string[] string) {
System.out.println ("123");
}*/
/**
* can be compiled but not run
* @param args
*/
public static void main (String args) {
System.out.println (" 123 ");
}
/**
* Cannot compile, the return value must be void
* @param args
* @return
*/
/*static public int main (string[] args) {
}*/
/**
* can be compiled but not run
*/
static void Main (string[] args) {
System.out.println (123);
}
}