Code block (or initialization block) of class members

1. The function of the code block: used to initialize classes and objects
 * 2. If the code block is modified, it can only be static.
 * 3. Classification: static code block vs non-static code block
 * 
 * 4. Static code block
 *> There can be output statements inside
 *> are executed with the loading of the class, and only executed once
 *> Function: initialize the information of the class
 *> If multiple static code blocks are defined in a class, they will be executed in the order of declaration
 *> The execution of static code blocks should take precedence over the execution of non-static code blocks
 *> Static code blocks can only call static properties and static methods, and cannot call non-static structures
 * 
 * 5. Non-static code blocks
 *> Internal can have Output statement
 *> Executed with the creation of the object
 *> Each time an object is created, a non-static code block is executed
 *> Function: You can initialize the properties of the object when the object is created
 *> If defined in a class Multiple non-static code blocks are executed in the order of declaration
 *> Static properties, static methods, or non-static properties and non-static methods can be called in non-static code blocks

public class BlockTest {
	public static void main(String[] args) {
		
		String desc = Person.desc;
		System.out.println(desc);
		
		Person p1 = new Person();
		Person p2 = new Person();
		System.out.println(p1.age);
		
		Person.info();
	}
}


class Person{
	//属性
	String name;
	
	int age;

	static String desc = "我是一个人";
	
	//构造器
	public Person(){
		
	}
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	//非static的代码块
	{
		System.out.println("hello, block - 2");
	}
	{
		System.out.println("hello, block - 1");
		//调用非静态结构
		age = 1;
		eat();
		//调用静态结构
		desc = "我是一个爱学习的人1";
		info();
	}
	//static的代码块
	static{
		System.out.println("hello,static block-2");
	}
	static{
		System.out.println("hello,static block-1");
		//调用静态结构
		desc = "我是一个爱学习的人";
		info();
		//不可以调用非静态结构
//		eat();
//		name = "Tom";
	}
	
	//方法
	public void eat(){
		System.out.println("吃饭");
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public static void info(){
		System.out.println("我是一个快乐的人!");
	}
	
}

 hello,static block-2
hello,static block-1
I am a happy person!
I am a person who loves to learn
hello, block-2
hello, block-1
eat.
I am a happy person!
hello, block-2
hello, block-1
Eat
I am a happy person!
1
I am a happy person!

From father and son, static first

//总结:由父及子,静态先行
class Root{
	static{
		System.out.println("Root的静态初始化块");
	}
	{
		System.out.println("Root的普通初始化块");
	}
	public Root(){
		super();
		System.out.println("Root的无参数的构造器");
	}
}
class Mid extends Root{
	static{
		System.out.println("Mid的静态初始化块");
	}
	{
		System.out.println("Mid的普通初始化块");
	}
	public Mid(){
		super();
		System.out.println("Mid的无参数的构造器");
	}
	public Mid(String msg){
		//通过this调用同一类中重载的构造器
		this();
		System.out.println("Mid的带参数构造器,其参数值:"
			+ msg);
	}
}
class Leaf extends Mid{
	static{
		System.out.println("Leaf的静态初始化块");
	}
	{
		System.out.println("Leaf的普通初始化块");
	}	
	public Leaf(){
		//通过super调用父类中有一个字符串参数的构造器
		super("尚硅谷");
		System.out.println("Leaf的构造器");
	}
}
public class LeafTest{
	public static void main(String[] args){
		new Leaf(); 
		System.out.println();
		new Leaf();
	}
}

Root's static initialization block
Mid's static initialization block
Leaf's static initialization block
Root's normal initialization block
Root's parameterless constructor
Mid's normal initialization block
Mid's parameterless constructor
Mid's parameterless constructor, its parameter value:
The normal initialization block
Leaf constructor of Silicon Valley Leaf

Root's normal initialization block
Root's non-parameter constructor
Mid's normal initialization block
Mid's parameterless constructor
Mid's parameter constructor, its parameter value:
the normal initialization block
Leaf constructor of Silicon Valley Leaf

class Father {
	static {
		System.out.println("11111111111");
	}
	{
		System.out.println("22222222222");
	}

	public Father() {
		System.out.println("33333333333");

	}

}

public class Son extends Father {
	static {
		System.out.println("44444444444");
	}
	{
		System.out.println("55555555555");
	}
	public Son() {
		System.out.println("66666666666");
	}


	public static void main(String[] args) { // 由父及子 静态先行
		System.out.println("77777777777");
		System.out.println("************************");
		new Son();
		System.out.println("************************");
		new Son();
		System.out.println("************************");
		new Father();
	}

}

 11111111111
44444444444
77777777777
************************
22222222222
33333333333
55555555555
66666666666
************************
22222222222
33333333333
55555555555
66666666666
************************
22222222222
33333333333

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108989838