- Basic introduction
A coded block, also known as an initialization block, is a member of a class [that is, a part of a class], which is similar to a method. It encapsulates logical statements in the method body and is surrounded by {}.
But unlike methods, there is no method name, no return, no parameters, only method body, and it is not explicitly called through objects or classes, but implicitly called when loading classes or creating objects.
- Basic grammar
[modifier]{
code
};
Note:
1) Modifier is optional. If you want to write, you can only write static
2) Code blocks are divided into two categories: static code blocks with static modification and ordinary code blocks without static modification
3) The code can be any logical statement (input, output, method call, loop, judgment, etc.)
4); The number can be written or omitted.
- Applicable scenarios for code blocks
When there are enough constructors of a class to form an overload, in order to reduce the duplication of content to be executed by each constructor and reduce code redundancy. give an example:
package com.kxy.codeBlock; class Movie{ String name; double price; String director; { System.out.println("The movie begins"); System.out.println("The advertisement began"); System.out.println("The movie is over"); } public Movie(String name) { super(); this.name = name; System.out.println("Movie(name)Constructor called..."); } public Movie(String name,double price) { super(); this.name = name; this.price = price; System.out.println("Movie(name,price)Constructor called..."); } public Movie(String name, double price, String director) { super(); this.name = name; this.price = price; this.director = director; System.out.println("Movie(name,price,director)Constructor called..."); } } public class CodeBlock1 { public static void main(String[] args) { Movie Movie1=new Movie("Hello,Li Huanying"); Movie Movie2=new Movie("Changjin Lake",100,"xxx"); Movie Movie3=new Movie("Speed and passion 9",49); } }
The movie begins The advertisement began The movie is over Movie(name)Constructor called... The movie begins The advertisement began The movie is over Movie(name,price,director)Constructor called... The movie begins The advertisement began The movie is over Movie(name,price)Constructor called...
Summary:
(1) The code block is suitable for use when the constructor is overloaded
(2) Code blocks take precedence over constructor execution
(3) The code block will be executed automatically in each object
The role and essence of code blocks: initializing constructors.
Let's study and discuss the advanced details of static code blocks and ordinary code blocks:
package com.kxy.codeBlockDetail_; //Code block advanced details: 01: class B{ { System.out.println("I am B Class, I was executed..."); } public B() { super(); System.out.println("B()The constructor was called..."); } } public class codeBlockDetail01 { public static void main(String[] args) { //For static code blocks: //Static code blocks are executed as classes are loaded, so when are classes loaded? //1. When new an object, the class is loaded B b=new B(); } }
I am B Class, I was executed... B()The constructor was called...
When new an object, the class is loaded. The code block is then executed and the constructor is called. Priority: (code block > constructor)
package com.kxy.codeBlockDetail_; //Code block advanced details: 01: class A { static{ System.out.println("I am A Class, I was executed..."); } public A() { super(); System.out.println("A()The constructor was called..."); } } class B extends A { static{ System.out.println("I am B Class, I was executed..."); } public B() { super(); System.out.println("B()The constructor was called..."); } } public class codeBlockDetail01 { public static void main(String[] args) { // For static code blocks: // Static code blocks are executed as classes are loaded, so when are classes loaded? // 2. When a subclass inherits from the parent class, the parent class loads first and then the subclass B b = new B(); } }
I am A Class, I was executed... A()The constructor was called... I am B Class, I was executed... B()The constructor was called...
When there is an inheritance relationship, according to the essence of inheritance, the base class is loaded first, and then the subclasses are loaded in turn.
package com.kxy.codeBlockDetail_; //Code block advanced details: 01: class B { public static int a=100; static{ System.out.println("I am B Class, I was executed..."); } public B() { System.out.println("I am B The constructor was called by me..."); } } public class codeBlockDetail01 { public static void main(String[] args) { // For static code blocks: // Static code blocks are executed as classes are loaded, so when are classes loaded? // 1. When new an object, the class is loaded // 2. When a subclass inherits from the parent class, the parent class loads first and then the subclass //3. When accessing static members, the class is loaded System.out.println(B.a); } }
I am B Class, I was executed... 100
3. When accessing a static member, the class is loaded. At this time, the static code block is executed first, and then the static member is output. Priority: (static code block > static member)
Summary:
1. When new an object, the class is loaded
2. When a subclass inherits from the parent class, the parent class loads first and then the subclass
3. When accessing static members, the class is loaded
Difference between static code block and ordinary code block:
1. Different execution times: static code block can only be executed once, while ordinary code block new is executed once for an object.
2. Static code blocks are executed with the loading of classes, while ordinary code blocks are executed with the creation of objects (before the constructor is called).
3. When accessing a static member of a class, the static code block will be executed, but the ordinary code block will not be executed (reason: the static code block is executed with the loading of the class, but the ordinary code block has nothing to do with the loading of the class, so when accessing a static member, the ordinary code block will not be executed).