Examples of common code is Java code blocks, the code block structure, the difference between the static code block, the order of execution

Examples of common code is Java code blocks, the code block structure, the difference between the static code block, the order of execution

In addition to executing sequentially said common code block, static code block, code block structure, there is a static method, a static variables are then put together, the execution order, how to determine.

I actually get hold of the code, look at the execution order.

 

[java]  view plain  copy
 
 
 
 
  1. public class Line {  
  2.     static {  
  3.         System.out.println ( "static block of code: loading line");  
  4.     }  
  5.   
  6.     public static String s = getString();  
  7.   
  8.     private static String getString() {  
  9.         System.out.println ( "static variables assigned to the static method of execution: loading line");  
  10.         return "ss";  
  11.     }  
  12.   
  13.     public static void test() {  
  14.         System.out.println ( "ordinary static method to perform: loading line");  
  15.     }  
  16.   
  17.     public Line() {  
  18.         System.out.println ( "constructor method performed: loading line");  
  19.     }  
  20.   
  21.     {  
  22.         System.out.println ( "block of code structure");  
  23.     }  
  24. }  

 

 

Then the main method

 

[java]  view plain  copy
 
 
 
 
  1. /** 
  2.  * Block execution order of test 
  3.  * <p> 
  4.  * Created by lxk on 2017/2/21 
  5.  */  
  6. public class CodeBlockTest {  
  7.     public static void main(String[] args) {  
  8.         System.out.println ( "master method");  
  9.         {  
  10.             System.out.println ( "the beginning of the main process, normal code execution block");  
  11.         }  
  12.         Line line = new Line();  
  13.         System.out.println("...............");  
  14.         Line line1 = new Line();  
  15.         System.out.println("...............");  
  16.         {  
  17.             System.out.println ( "main method ends things, normal code execution block");  
  18.         }  
  19.     }  
  20. }  

See the results.

 



For the implementation of the above results, the concept of a few simple description.

 

Common code block:

Occurring in or {statement} method is called common code block.
Common code block and general statement execution order is determined by the order in which they appear in the code - "first appeared before execution"

 

 

Configuration code block:

Directly defined in the class and there is no increase static keyword {} block of code is called a code block configuration.
Code block is configured to be called when the object is created each time will be called to create an object, and the execution order of the code block structure in preference to class constructor.
The order of execution of the code block construction method will not affect the location, I deliberately put him in after the constructor.

 

 

Static code block:

Using the code block declared static keyword in java.
Block is used to initialize static class initialization for the class attributes. Each static block of code is executed only once.
Since the JVM executes a static block if the class is loaded, the static code block to execute the main method.
If the class contains a plurality of static code block, according to the "first execution code is first defined, the code execution definition." (I did not make this test)
Note:
1 static code block can not exist within any method.
2 static block of code can not directly access static instance variables and instance methods, by way of example need to access the object class.
 
This is what I see on CSDD blog, the interview has encountered this problem. If infringement, please contact!

Guess you like

Origin www.cnblogs.com/FanKL/p/10987932.html