Java understands the main method, java understands the main

Source: Internet
Author: User

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?
  1. Public class Test4 {
  2. Public static void main (String [] args ){
  3. Fun ();
  4. }
  5. Public static void fun (){
  6. System. out. println ("hello ");
  7. }
  8. }



 

It can be called directly by the main method.

It can also be called through an object.

[Java]View plaincopyprint?
  1. Public class Test4 {
  2. Public static void main (String [] args ){
  3. New Test4 (). fun ();
  4. }
  5. Public void fun (){
  6. System. out. println ("hello ");
  7. }
  8. }



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?
  1. Public class Test4 {
  2. Public static void main (String [] args ){
  3. For (int I = 0; I <args. length; I ++ ){
  4. System. out. println ("Number" + I + "parameter" + args [I]);
  5. }
  6. }
  7. }



 

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.

Related Article

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.