1 Common code block

Is the body of a method in a class

  public void xxx(){ 
      //code 
  } 

1234
Copy the code

2 building blocks

A code snippet wrapped in {}, the constructor block is called when the object is created, every time the object is created, and takes precedence over the class constructor. Variables defined in a constructor block are local variables.

  { 
     //code 
  } 

1234
Copy the code

3 a static block

  static{  
     //code 
  } 

1234
Copy the code

4 Synchronize code blocks

Use code blocks wrapped with synchronized(obj){}. In a multi-threaded environment, read and write operations on shared data need to be mutually exclusive; otherwise, data inconsistency will occur. Synchronized is commonly used to modify methods, and its semantics are that any thread entering synchronized needs to acquire the object lock first. If it is occupied, it will be blocked, realizing mutually exclusive access to shared resources. Synchronized comes at a price. A common scenario is a lengthy method in which only a small piece of code needs to access a shared resource. Using a synchronized block, this small piece of code is wrapped in a synchronized block, both to achieve synchronized access and to reduce the overhead that synchronization introduces. Synchronized code blocks must be written in methods.

    synchronized(obj){ 
        //code 
    } 

1234
Copy the code

Five instances

Public static void main(String[] args) {new Test(); Println ("done.."); // New a class, but anonymous code blocks must be processed before new. ); Test.run(); } static int num = 4; Static {system.out. println("num:"+num); A system.out.println ("a"); } { num += 3; System.out.println("b:"+num); } int a = 5;} int a = 5; Println ("c:"+a); system.out.println ("c:"+a); C} Test() {// The constructor of class c} Test() {system.out.println ("d"); } static void run() {system.out.println ("e"); }}Copy the code

Run:

num:4
a
b:7
c:5
d
done..
e
Copy the code

6 summarizes

If the class has not been loaded: 1. Initialize the parent class’s static code block and static variable first, and execute the static code block and static variable only in the order in which the code appears. Perform subclass static code block and static variable initialization. 5. Initialize instance variables of subclasses. 6

  • Static code block (only loaded once)

  • Constructor (create an instance and load it once)

  • Static methods are invoked

Please pay attention to the public number “programmer interview way” reply to “interview” to get a complete set of interview package!!

This public account to share their own from programmer xiao Bai to experience spring recruitment autumn recruitment cut more than 10 offer interview written test experience, including [Java], [operating system], [computer network], [design mode], [data structure and algorithm], [Dacheng face by], [database] look forward to you join!!

1. Computer network —- Three times handshake four times Wave 2. Dream come true —– Project self-introduction 3. 4. Shock! Check out this programmer interview manual!! 6. Nearly 30 interviews share 7. I got the free book you asked for