Public static void main (String [] args)

Source: Internet
Author: User

When I first came into contact with the Java program, the teacher taught us to start learning from the following sentence. It is said that it is customary. So until today, I still only know that the java program should be written like this, I am ashamed of the specific reason.

                  System.out.println("Hello Java"  }

Since I am so ashamed of myself today, it is time to understand why the main functions of java should be written in this format. Let's explain from the following points.

Why use public static to modify main ()

In Java, the main () method is the entry method of java applications. The Java virtual machine uses the main method to locate the running program to be started, and checks whether the class of the main function is loaded by the Java Virtual Machine. If it is not loaded, the class is loaded and all other related classes are loaded. Therefore, when the program is running, the first method to execute (note that it is a method, rather than other methods, such as code blocks) is the main () method. Generally, if you want to run a class method, you must first instantiate an object of the class and then use the "Object Name. method Name () "to run the method, but because main is the entry of the program, the object has not yet been instantiated, so the main method is declared as static, in this way, you can directly use the class name. method.

Why use void to modify main ()

  

Description of String [] args

 

 

The main () method can throws Exception.

 

When I saw that the main () method could not throw an exception on the Internet and felt a bit confused, I tried it myself. The Code is as follows, and it runs normally.

 

 

          main(String[] args)          System.out.println("Hello World"           Exception("hello world"  }

 

Summary

By describing the modifier of main (), we finally understand all of the parameters and do not understand them at all. But there is still some confusion. Stay here and hope you can advise me. Q: Why is the static code block in the Code executed before the main () function?

 "Hello Wordld!"  

 

 

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.