当前位置:网站首页>如何理解:普通代码块、构造块、静态块?有什么关系?
如何理解:普通代码块、构造块、静态块?有什么关系?
2022-07-23 16:08:00 【陈亦康】
目录
普通代码块(本地代码块)
定义在方法里面用{ }括起来的代码块(凡是用{ }括起来都叫代码块,只是没有细分),就是普通代码块,也叫本地代码块 。(不多见,也不常用)
例如:
public class Test {
public static void main(String[] args){
{
System.out.println("本地代码块!");
}
}
}构造块(实例代码块)
实例代码块定义在类的内部,方法的外部,可以初始化实例的成员
例如:
class Student{
//成员变量
public String name;
public int score;
//类变量
public static String classes = "火箭6班";
//类方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正确
System.out.println("类方法");
}
//成员方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//实例代码块
{
this.name = "jay";
System.out.println("实例代码块!");
}
}静态块(静态代码块)
静态代码块定义在类的内部,方法的外部,可以初始化实例的成员
例如:
class Student{
//成员变量
public String name;
public int score;
//类变量
public static String classes = "火箭6班";
//类方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正确
System.out.println("类方法");
}
//成员方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//实例代码块
{
System.out.println("实例代码块!");
}
//静态代码块
static{
classes = "16班";
System.out.println("静态代码块!");
}他们之间有什么关系呢?
例子:
情况一:看如下代码会执行什么?
class Student{
//成员变量
public String name;
public int score;
//构造方法
public Student(){
System.out.println("构造方法!");
}
//类变量
public static String classes = "火箭6班";
//类方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正确
System.out.println("类方法");
}
//成员方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//实例代码块
{
System.out.println("实例代码块!");
}
//静态代码块
static{
System.out.println("静态代码块!");
}
}
public class Test {
public static void main(String[] args){
Student stu = new Student();
}
}情况一运行结果:

情况二:看如下代码会执行什么?
class Student{
//成员变量
public String name;
public int score;
//构造方法
public Student(){
System.out.println("构造方法!");
}
//类变量
public static String classes = "火箭6班";
//类方法
public static void fun1(){
Student stu = new Student();
stu.name = "jay"; //正确
System.out.println("类方法");
}
//成员方法
public void fun2(String name, int score){
this.name = name;
this.score = score;
}
//实例代码块
{
System.out.println("实例代码块!");
}
//静态代码块
static{
System.out.println("静态代码块!");
}
}
public class Test {
public static void main(String[] args){
System.out.println(Student.classes);
}
}情况二运行结果:
分析:
- 他们执行的顺序是:静态代码块 -> 实例代码块 -> 构造方法(不论代码块的位置在哪,都是这个顺序!若有多个静态,看定义顺序即可)
- 只要加载了类,静态代码块就会被执行
- 若没有实例化对象只会执行静态的
- 若有多个实例,须看定义顺序
- 如果没有实例化对象,静态代码块只会执行一次
边栏推荐
- JS: image URL to Base64 encoding
- 变分法 (Calculus of Variations)
- MySQL事务,从redo log、bin log、undo log说起...
- 数字安全巨头Entrust披露六月遭到勒索软件团伙攻击
- Role definition in USB type-C PD CC logic chip
- Non inherited polymorphic ideas cooperate with typeID to realize the transmission of different parameters
- 悲观锁和乐观锁
- 本周一问 | Vivado 综合阶段什么约束生效?
- Detailed explanation of CO process principle in go
- Redis data loss problem
猜你喜欢

Information theory: introduction and information measures

Calculus of variations

CSDN custom T-shirts are waiting for you to get, and the benefits of new programmer are coming!

go中的协程原理详解

MySQL 7 kinds of join (Figure)

How does memory alignment in go optimize program efficiency?

微服务雪崩问题及解决方案

Use moment to get the date of the current day and the next day

Sentinel installation diagram

Go medium high parallel communication mode: the underlying principle of channel pipeline
随机推荐
如何评估券商分析师预测股票涨跌的准确性?
【216】go语言标准库包名
【JZOF】13机器人的运动范围
JS: image URL to Base64 encoding
数据库建模
Use moment to get the date of the current day and the next day
go中高并发下的锁是如何工作的(结合源码)
阿里 P7 到底是怎样的水平?
华为胖瘦AP切换方法
Three barriers in the workplace: annual salary of 300000, 500000 and 1million
MySQL foundation and performance optimization
MySQL 66 questions, 20000 words + 50 pictures, including (answer analysis)
Rhcsa notes 5
The Little Schemer-周而复始之Y组合子由来
go中的協程原理詳解
Installation and configuration tutorial of mingw-w64
[215] Gin Framework connection to MySQL Database
【JZOF】13機器人的運動範圍
Pdman, a database modeling tool comparable to PowerDesigner [easy to understand]
“如今,99.9% 以上的代码都是垃圾!”
