"Java" outputs Hello World before the main function

Source: Internet
Author: User

In the Java language, the main () method is the entry for the entire program, which is the main () method that is loaded first at run time, but this does not mean that the main () method is the first module to be executed when the program is run.

In the Java language, a static block of code is called when the class is loaded, so it can be executed before the main () method, using a static block of code to output Hello World before the main function

public class Staticc {static {System.out.println ("Hello World");    } public static void Main (string[] arg) {System.out.println ("Say hello to the world"); }}

Output:

Hello World
Say hello to the world

The execution order is independent of the location of the static code block

public class Staticc {/*static {System.out.println ("Hello World");    } */public static void Main (string[] arg) {System.out.println ("Say hello to the world");    } static {System.out.println ("Hello World"); }}

Output:

Hello World
Say hello to the world

"Java" outputs Hello World before the main function

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.