Java understands the main method, java understands the main
Understanding the main method
Method Definition Format
Public static return value type method name (parameter list)
The static keyword is added to the previous method definition. Why do we need to add the static keyword?
[Java]View plaincopyprint?
- Public class Test4 {
- Public static void main (String [] args ){
- Fun ();
- }
- Public static void fun (){
- System. out. println ("hello ");
- }
- }
It can be called directly by the main method.
It can also be called through an object.
[Java]View plaincopyprint?
- Public class Test4 {
- Public static void main (String [] args ){
- New Test4 (). fun ();
- }
- Public void fun (){
- System. out. println ("hello ");
- }
- }
Main Method
Public static void main (String args [])
Public: indicates that this method can be called externally.
Static: indicates that this method can be called directly by the class name.
Void: The main method is the starting point of the program, so no return value is required.
Main: The system specifies the name of the method called by default. The main method name is found by default during execution.
String args []: indicates the runtime parameter. The parameter is transmitted in the form of java class name parameter 1 parameter 2 parameter 3 ..
[Java]View plaincopyprint?
- Public class Test4 {
- Public static void main (String [] args ){
- For (int I = 0; I <args. length; I ++ ){
- System. out. println ("Number" + I + "parameter" + args [I]);
- }
- }
- }
Parameter settings are added during execution.
Java class name parameter 1 parameter 2...
Enter a space in the middle of hello world.
If you want a space in the middle, you can enclose it in double quotation marks.