当前位置:网站首页>How to understand: common code block, construction block, static block? What does it matter?
How to understand: common code block, construction block, static block? What does it matter?
2022-07-23 18:53:00 【Chen Yikang】
Catalog
Common code block ( Local code block )
Tectonic block ( Instance code block )
A static block ( Static code block )
What's the relationship between them ?
Common code block ( Local code block )
The definition is used in the method { } Enclosed code block ( In general { } It is called code block , Just no segmentation ), It's a normal code block , Also called native code blocks .( Not often , Not often )
for example :
public class Test {
public static void main(String[] args){
{
System.out.println(" Local code block !");
}
}
}Tectonic block ( Instance code block )
The instance code block is defined inside the class , The outside of the method , Members of the instance can be initialized
for example :
class Student{
// Member variables
public String name;
public int score;
// Class variables
public static String classes = " The rockets 6 class ";
// Class method
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; // correct
System.out.println(" Class method ");
}
// Member method
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
// Instance code block
{
this.name = "jay";
System.out.println(" Instance code block !");
}
}A static block ( Static code block )
Static code blocks are defined inside the class , The outside of the method , Members of the instance can be initialized
for example :
class Student{
// Member variables
public String name;
public int score;
// Class variables
public static String classes = " The rockets 6 class ";
// Class method
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; // correct
System.out.println(" Class method ");
}
// Member method
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
// Instance code block
{
System.out.println(" Instance code block !");
}
// Static code block
static{
classes = "16 class ";
System.out.println(" Static code block !");
}What's the relationship between them ?
Example :
Situation 1 : See what the following code will execute ?
class Student{
// Member variables
public String name;
public int score;
// Construction method
public Student(){
System.out.println(" Construction method !");
}
// Class variables
public static String classes = " The rockets 6 class ";
// Class method
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; // correct
System.out.println(" Class method ");
}
// Member method
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
// Instance code block
{
System.out.println(" Instance code block !");
}
// Static code block
static{
System.out.println(" Static code block !");
}
}
public class Test {
public static void main(String[] args){
Student stu = new Student();
}
}Case 1 operation results :

Situation two : See what the following code will execute ?
class Student{
// Member variables
public String name;
public int score;
// Construction method
public Student(){
System.out.println(" Construction method !");
}
// Class variables
public static String classes = " The rockets 6 class ";
// Class method
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; // correct
System.out.println(" Class method ");
}
// Member method
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
// Instance code block
{
System.out.println(" Instance code block !");
}
// Static code block
static{
System.out.println(" Static code block !");
}
}
public class Test {
public static void main(String[] args){
System.out.println(Student.classes);
}
}Case 2 operation results :
analysis :
- The order of their execution is : Static code block -> Instance code block -> Construction method ( No matter where the code block is , This is the order ! If there are multiple static , Just look at the definition order )
- As long as the class is loaded , Static code blocks are executed
- If the object is not instantiated, it will only execute statically
- If there are multiple instances , It depends on the order of definition
- If the object is not instantiated , Static code blocks are executed only once
边栏推荐
- 代码整洁,高效的系统方法
- LeetCode 0131. 分割回文串
- My creation anniversary
- 【3D建模制作技巧分享】Zbrush如何将图片转浮雕模型
- moxa串口服务器型号,moxa串口服务器产品配置说明
- Where should we start to learn modeling from zero foundation? How to learn game modeling well?
- 一文了解 NebulaGraph 上的 Spark 项目
- [2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz
- 作为3D资深建模师给刚入门学习伙伴的一些建议,用起来
- 【游戏建模模型制作全流程】3ds Max和ZBrush制作无线电接收器
猜你喜欢

Google正在改进所有产品中的肤色表现 践行“图像公平”理念
![[2020] [paper notes] Based on Rydberg atom——](/img/5c/186cae4e47a236ae4062d15f839196.png)
[2020] [paper notes] Based on Rydberg atom——

有人是靠自学建模找到工作的吗?千万别让这些思维害了你

到底适不适合学习3D建模?这5点少1个都不行
![Multithreading [comprehensive study of graphics and text]](/img/70/8a1227b2159349cf25a85dff8f9d1c.png)
Multithreading [comprehensive study of graphics and text]

Use three JS realize the 'ice cream' earth, and let the earth cool for a summer

Interviewer: what do you think is your biggest weakness?

Redis【2022最新面试题】

【2022】【论文笔记】太赫兹量子阱——

零基础要学建模该从何开始?如何才能学好游戏建模?
随机推荐
世界上最大的开源基金会 Apache 是如何运作的?
[sharing 3D modeling and production skills] how ZBrush turns pictures into relief models
JUC concurrent programming [detailed explanation and demonstration]
代码整洁,高效的系统方法
Integer and = = compare
ZigBee集成开发环境IAR安装
Where should we start to learn modeling from zero foundation? How to learn game modeling well?
一文详解:TMP1750芯片 三通道线性LED驱动器
怎么将word中的times new roman的双引号替换成宋体双引号
Leetcode 0131. split palindrome string
Crack WiFi password with Kail
JUC并发编程【详解及演示】
Have a safe summer vacation, no holidays! Please keep these summer safety tips
How to realize the digital transformation of the banking industry
Paddlenlp's UIE classification model [taking emotional propensity analysis news classification as an example] including intelligent annotation scheme)
Gradle【图文安装及使用演示 精讲】
C#启动程序传递参数丢失双引号,如何解决?
我的创作纪念日
Spark 安装与启动
How does the NiO mechanism of jetty server cause out of heap memory overflow
