Teaching Girl to Learn Java (34): Code Initialization Block

Hello, I am the second silent king, (currently) the top ten blog expert in CSDN Week. This is the thirty-fourth part of the column "Teach Girls to Learn Java". Today we will talk about the code initialization block in Java-what are the functions of the code initialization block? Is the constructor executed early or the code initialization block?

The code in this column has been included in  GitHub  github.com/itwanger  , and there are also interview questions I prepared carefully for you.

The third sister has started school and learned computer software programming. Her school is very close to my home, and it is only 10 stops away by bus. She will come to me every weekend and ask me to help her learn Java. As a programmer with more than ten years of programming experience, coupled with the blood relationship given to us by our parents, I feel obligated.

"Second brother, what we are going to learn today is the'code initialization block', right?" It seems that the third sister has previewed the homework I left for her last time.

"Yes, Sanmei. The code initialization block is used to initialize some member variables (fields)." I answered her with an unpretentious smile, "The code initialization block will be executed when creating an object through a class."

"You can directly initialize member variables through the'=' operator, but you can do more things through the code initialization block, such as printing out the initialized value of the member variable."

----The dividing line of the Eight Classics of Zhenger, the beginning of the text ------------

01. What is the function of the code initialization block

In the following example, we can directly pass <

Guess you like

Origin blog.csdn.net/qing_gee/article/details/108729837