The use of java code block structure


The content of the article is selected from Shang Silicon Valley, jdk8, eclipse environment

Features of code blocks

  • The code block is one of the internal members of the class and can only be modified with the static keyword. A static code block is called a static code block.
  • The code block is written as a brace {}, and then the initialization code is written inside the brackets.
  • The code block is also called the initialization block, which is generally used to assign initial values ​​to the properties of the class.
  • The code in the static code block can be executed when the class is loaded, and the code in the non-static code block can be executed when the object is created.
  • The code in a static code block can only call static properties and methods, and cannot call non-static properties and methods. Non-static code blocks can call static properties and methods, as well as non-static properties and methods.

Usage of code blocks

Static code block

A static code block is a code block modified with the static keyword at the front of the code block. The
code is as follows

package com.atguigu.java3;

import com.atguigu.java2.MainTest;

public class BlockTest {
    
    
	public static void main(String[] args) {
    
    
		String str = Person.desc;
	}
}

class Person{
    
    
	int age;
	String name;
	static String desc ="我是一个人";
	
	public Person(){
    
    
		
	}
	
	public Person(String name,int age){
    
    
		this.name = name;
		this.age = age;
	}
	
	//静态代码块
	static{
    
    
		System.out.println("hello,static block");
	}
	
	//非静态代码块
	{
    
    
		System.out.println("hello block");
	}
	
	public void eat(){
    
    
		System.out.println("人要吃饭");
	}

	@Override
	public String toString() {
    
    
		return "Person [age=" + age + ", name=" + name + "]";
	}
	
	public static void info(){
    
    
		System.out.println("我是一个快乐的人");
	}
}

The result of the operation is

hello,static block

The static code block modified by the static keyword means that the content of the code block is also loaded into the method area of ​​the class when the information of the class is loaded into the method area, and the difference between the code block and the method is that it Can be executed immediately.

When the class is loaded, the content of the static code block is also executed immediately. In the code, String str = Person.desc; This statement calls a static property, and at this time, it actually loads the class information first, and then calls the static property. The content of the static code block is executed with the loading of the class, so the output hello, static block.

Non-static code block

Similar to static code blocks, non-static code blocks are executed when the object is created.

Create two objects inside the main method

	public static void main(String[] args) {
    
    
		String str = Person.desc;
		Person p1 = new Person();
		Person p2 = new Person();
	}

The result is

hello,static block
hello block
hello block

Use of code blocks

Static code blocks are generally executed only once, because classes are generally only loaded once. Non-static code blocks can be executed multiple times because a class can create multiple objects.

Code blocks can be used to assign values ​​to the properties of objects.
For example, add an assignment statement to a non-static code block:

	//非静态代码块
	{
    
    
		System.out.println("hello block");
		age = 1;
	}
	public static void main(String[] args) {
    
    
		String str = Person.desc;
		Person p1 = new Person();
		Person p2 = new Person();
		System.out.println(p1.age);
	}

At this time, the running result of the main method is

hello,static block
hello block
hello block
1

In the same way, static code blocks can also assign values ​​to static properties. such as:

	//静态代码块
	static{
    
    
		System.out.println("hello,static block");
		desc = "aabb";
	}
	public static void main(String[] args) {
    
    
		String str = Person.desc;
		System.out.println(Person.desc);
		Person p1 = new Person();
		Person p2 = new Person();
		System.out.println(p1.age);
	}

The result of the operation is

hello,static block
aabb
hello block
hello block
1

It can be seen that the assignment of the static code block overrides the explicit assignment in the static attribute.

ps: Multiple code blocks and static code blocks can be defined in the class. The order of execution is the order of definition, and the execution of static code blocks takes precedence over the execution of non-static code blocks. Determined by the creation of the object. Although multiple code blocks can be defined, it is generally not necessary to define multiple code blocks. It is not necessary to merge multiple code blocks into one.

The order in which the attributes are assigned

The attribute assignment can have

  1. Default initialization (integer type is initialized to 0, reference type is initialized to null)
  2. Display initialization (initialize when declaring properties)
  3. Initialization in the constructor
  4. After having the object, initialize it through the object. Attribute or object. Method

The order of the above four methods is in the order of initialization. The first is the original initialization method, which can be understood as a lower priority, that is, the latter initialization method will overwrite the previous initialization method.

Now there is one more method, that is, to initialize the attributes inside the code block. The priority of this initialization method is at the same priority as the display initialization method.

package com.atguigu.java3;

public class OrderTest {
    
    
	public static void main(String[] args) {
    
    
		Order order = new Order();
		System.out.println(order.orderID);
	}
}

class Order{
    
    
	int orderID = 3;
	
	{
    
    
		orderID = 4;
	}
	
}

The result is 4

However, if you exchange the following sequence of display initialization and code block initialization

class Order{
    
    

	{
    
    
		orderID = 4;
	}
	
	int orderID = 3;
	
}

The output result is 3

It can be seen that the output result only depends on the defined sequence, so the priority of code block initialization and display initialization are the same

  1. Default initialization (integer type is initialized to 0, reference type is initialized to null)
  2. Display initialization (initialization when declaring attributes)/code block initialization
  3. Initialization in the constructor
  4. After having the object, initialize it through the object. Attribute or object. Method

Guess you like

Origin blog.csdn.net/Meloneating/article/details/114160827