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.