static block and non-static block(constructor block)

想来想去,先来一题比较好

public class Foo {
public static void main(String[] args) {
Baz.testAsserts();
Baz.testAsserts();
// Will execute after Baz is initialized.
}
} class Bar {
static {
Baz.testAsserts();
// Will execute before Baz is initialized!
}
} class Baz extends Bar {
static int x = 1; static void testAsserts() { System.out.println("x is " + x);
x=x+2;
} }
  • 输出结果

x is 0

x is 1

x is 3

  • 分析
  1. Invoking Baz.testAsserts() cause Baz to be initialized
  2. default value int x=0;
  3. Before Baz initialize , Bar must be initialized
  4. Bar's static initializer again invoking Baz.testAsserts()
  5. so x is 0 , then x+2 , x=2
  6. go on initializing Baz , init x = 1;
  7. Invoking Baz.testAsserts() x = 1 , so x is 1;

Java类初始化顺序

父类静态变量 ——>父类静态代码块——>子类静态代码块——>父类非静态变量 ——>

父类非静态代码块——>父类构造函数 ——>子类非静态变量——>子类非静态代码块——>

子类构造函数

非静态代码块 non-static block(constructor block)

class A {

    int x ;

    //block num 1
{
x = 1;
System.out.println("block num 1, x is " + x);
} A() {
x = 3;
System.out.println("constructor block x is " + x);
} //block num 2
{
x = 2;
System.out.println("block num 2, x is " + x);
} } public class Non_staticBlock { public static void main(String[] args) { String newLine = System.getProperty("line.separator"); System.out.println("====first time instantiate ====" + newLine);
new A(); System.out.println(" \n====second time instantiate ====" + newLine);
new A();
} }
  • 输出结果、顺序

====first time instantiate ====

block num 1, x is 1

block num 2, x is 2

constructor x is 3

====second time instantiate ====

block num 1, x is 1

block num 2, x is 2

constructor x is 3

  • 非静态代码块被java编译器拷贝到了构造块内,所以称为"constructor block"也是可以的,所以每次 new 构造函数也都执行
  • .class 文件如下, 非静态代码块被java编译器拷贝到了构造块内.
class A {
int x = 1; A() {
System.out.println("block num 1, x is " + this.x);
this.x = 2;
System.out.println("block num 2, x is " + this.x);
this.x = 3;
System.out.println("constructor x is " + this.x);
}
}

静态代码块 static block

class AA {

    AA() {
x = 3;
System.out.println("constructor x is " + x);
} static int x = 1; //block num 1
static {
System.out.println("static block num 1 , x is " + x);
} //block num 2
static {
x = 2;
System.out.println("static block num 2 , x is " + x);
} static void print() {
System.out.println("static method");
}
} public class StaticBlock {
static {
System.out.println("==== first ====");
} public static void main(String[] args) {
String newLine = System.getProperty("line.separator");
System.out.println("====AA class init ====" + newLine);
// class init
AA.print(); System.out.println(" \n====fisrt time instantiate AA====" + newLine); new AA();
System.out.println(" \n====sencond time instantiate AA====" + newLine);
new AA();
}
}
  • 输出结果、顺序

==== first ====

====AA class init ====

static block num 1 , x is 1

static block num 2 , x is 2

static method

==== first time instantiate AA ====

constructor x is 3

==== second time instantiate AA ====

constructor x is 3

  • 由于JVM在加载类时会执行静态代码块,且只会执行一次. 本例静态引用AA.print(); 触发类初始化
  • 静态代码块先于主方法执行,本例优先打印first
  • 更多内容搜索jvm类加载
  • .class 文件如下
class AA {
static int x = 1; AA() {
x = 3;
System.out.println("constructor x is " + x);
} static void print() {
System.out.println("static method");
} static {
System.out.println("static block num 1 , x is " + x);
x = 2;
System.out.println("static block num 2 , x is " + x);
}
}

联合看一下

class AAA {
int x; //block num 1
{
x = 1;
System.out.println("non-static block num 1 x is " + x);
} AAA() {
x = 3;
System.out.println("constructor x is " + x); } //block num 2
{
x = 2;
System.out.println("non-static block num 2 x is " + x);
} // The static block only gets called once,when the class itself is initialized,
// no matter how many objects of that type you create
static {
System.out.println("static block");
} //Gets called every time an instance of the class is constructed.
//the non-static block is actually copied by the Java compiler into every constructor the class has (source).
//So it is still the constructor's job to initialize fields.
//to understand "actually " , find the result in the .class file of A.class
{
System.out.println("non-static block");
}
} public class BlockSample { public static void main(String[] args) { String newLine = System.getProperty("line.separator"); System.out.println("====first time instantiate AAA ====" + newLine);
new AAA(); System.out.println(" \n====second time instantiate AAA ====" + newLine);
new AAA(); }
}
  • 输出结果、顺序

====first time instantiate AAA ====

static block

non-static block num 1 x is 1

non-static block num 2 x is 2

non-static block

constructor x is 3

====second time instantiate AAA ====

non-static block num 1 x is 1

non-static block num 2 x is 2

non-static block

constructor x is 3

  • .class 文件
class AAA {
int x = 1; AAA() {
System.out.println("non-static block num 1 x is " + this.x);
this.x = 2;
System.out.println("non-static block num 2 x is " + this.x);
System.out.println("non-static block");
this.x = 3;
System.out.println("constructor x is " + this.x);
} static {
System.out.println("static block");
}
}

java基础-静态,非静态(构造)代码块,类加载的更多相关文章

  1. java基础8 构造函数和构造代码块

    一.构造函数 1 构造函数的作用 给对应的对象进行初始化. 2 构造函数的格式 修饰符 函数名(形式参数){ //函数名就是类名 函数体 } 举例说明: class Perosn{ private i ...

  2. 黑马程序员——JAVA基础之构造函数,构造代码块

    ------- android培训.java培训.期待与您交流! ---------- 构造函数特点: 1.  函数名与类名相同 2.  不用定义返回值类型 3.  不可以写return语句 构造函数 ...

  3. java静态代码块、静态方法、静态变量、构造代码块、普通代码块

    一.静态代码块 1.在java类中(方法中不能存在静态代码块)使用static关键字和{}声明的代码块: public class CodeBlock{ static{ System.out.prin ...

  4. Java 基础 面向对象之关键字内部类代码块修饰符

    final final概念 继承的出现提高了代码的复用性,并方便开发.但随之也有问题,有些类在描述完之后,不想被继承,或者有些类中的部分方法功能是固定的,不想让子类重写.可是当子类继承了这些特殊类之后 ...

  5. 【Java基础】继承中的代码块和构造方法的执行顺序探索

    本文讲述有关一个类的静态代码块,构造代码块,构造方法的执行流程问题.首先来看一个例子 /** * Created by lili on 15/10/19. */ class Person{ stati ...

  6. java基础-表达式,语句和代码块

    浏览以下内容前,请点击并阅读 声明 表达式 表达式由变量,操作符和方法调用组成,表达式的返回值类型由表达式中的元素(如操作符,变量等)决定如: cadence = 0 上述代码将返回一个int类型的值 ...

  7. Java类中代码的执行顺序 静态代码块>构造代码块>构造方法

    一:静态代码块 注意是代码块,不是静态函数.函数要调用才执行,代码块加载就执行,一般是静态变量的声明与初始化.被static修饰的代码块(赋值.输出操作等).类中静态语句块仅在类加载时被执行一次 如 ...

  8. 【Java基础】2、Java中普通代码块,构造代码块,静态代码块区别及代码示例

    Java中普通代码块,构造代码块,静态代码块区别及代码示例.Java中普通代码块,构造代码块,静态代码块区别及代码示例 执行顺序:静态代码块>静态方法(main方法)>构造代码块>构 ...

  9. java基础之静态代码块,局部代码块,构造代码块区别。

    java中有几种常见的代码块,那怎样区别他们呢? 这里就这些问题,浅谈下我个人的理解. 1.局部代码块 局部代码块,又叫普通代码块.它是作用在方法中的代码块.例如: public void show( ...

随机推荐

  1. tcp链接断开的探测

    有资料说,read.write都可以探测tcp的断开,但都不是实时的. 但是实际在某些设备上测试发现,即使开一个线程每隔一小段时间发一次心跳包(write),write也不能探测连接已经断开,而且这个 ...

  2. DirectSound---输出设备基本操作(枚举、查询等)

    DirectSound是DirectX组件之一,提供了对音频设备的捕获和播放能力,同时它也是唯一几个支持Xp系统的音频技术之一. DirectSound主要有以下特点: 优点: 播放音频低延迟. 硬件 ...

  3. 使用xUnit为.net core程序进行单元测试(上)

    一. 导读 为什么要编写自动化测试程序(Automated Tests)? 可以频繁的进行测试 可以在任何时间进行测试,也可以按计划定时进行,例如:可以在半夜进行自动测试. 肯定比人工测试要快. 可以 ...

  4. jqueryui sortable拖拽后保存位置

    jqueryUI sortable 可以用来进行页面拖拽布局,然而有一个小问题就是拖拽后如何保存状态. 工作中遇到了这个情况,遍把这个问题记了下来,具体思路是: 利用拖拽stop后利用   var a ...

  5. C#操作MongoDB的简单实例

    最近比较忙,很久没更新了(虽然没人看,也没人在乎,也要记得be yourself), 前面分享了一些mongodb的安装和简单的语法,今天模仿支付宝首页的模块移动功能,用mongo做一个简单的后台实例 ...

  6. 一个大四毕业生想对自学Android的大学生说一些话

    本人大四,经历了秋招和春招,秋招拿了华为的android offer,春招是拿的java后台开发的offer,一路走来,感慨很多,有一些话想对在自学Android的大学生说.本文只是帮助像我一样的大学 ...

  7. 【Docker】安装Docker及基本使用

    该文以CentOS系统为例,介绍Docker安装及基本使用.为了简化安装流程,Docker 官方提供了一套安装脚本,CentOS 系统上可以使用这套脚本安装: curl -sSL https://ge ...

  8. Yii2基本概念之——属性(property)

    学习任何一门学问,往往都是从起基本的概念学起.万丈高楼平地起,这些基本概念就是高楼的基石,必须做详尽的分析.我们知道,Yii2是一款脉络清晰的框架,理顺了基础的概念和基本功能,学习更高级和复杂的功能就 ...

  9. Yii2.0源码阅读-一次请求的完整过程

    Yii2.0框架源码阅读,从请求发起,到结束的运行步骤 其实最初阅读是从yii\web\UrlManager这个类开始看起,不断的寻找这个类中方法的调用者,最终回到了yii\web\Applicati ...

  10. 【HTML_标签大全】

    HTML标签大全 标签 描述 标签类型 备注 <!--...--> 定义注释 / 单标签 <!DOCTYPE> 定义文档类型 / 单标签 <head></he ...