Compile a prank Java program

Source: Internet
Author: User

Compile a prank Java program

Author: Zhang guiquan

I wonder if you have realized how to use the Declaration of static variables in Java to write a "abnormal" program! See the following program:

/*

Hello. Java

*/

Public void Hello {

Static {

System. Out. println ("Hello, world! /N ");

System. Exit (0 );

}

}

Will this surprise you? After compilation and interpretation, the result is:

Hello, world!

 

The standard program code should be written as follows:

/*

Hello. Java

*/

Public void Hello {

Public static void main (string [] ARGs ){

System. Out. println ("Hello, world! /N ");

}

}

The previous program uses the static feature to process the Declaration and definition of static variables. The memory is allocated before the program is compiled to (or tested) the main function. What she met was a system. Out. println ("Hello, world! /N "); statement. So she processed the statement in advance. However, the system. Exit (0) Statement forces the program to end, so the main function detection will no longer be executed. If the system. Exit (0); statement is not added, you will see the annoying warning "the main function cannot be found! You know!

In fact, you can also perform lightweight code operations in the static code module, such as arithmetic operations and string processing. However, operations such as graphic interfaces cannot be implemented, it should be impossible to use static methods for processing.

 

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.