Java(静态)变量、(静态)代码块、构造方法的执行顺序
Java(静态)变量、(静态)代码块、构造方法的执行顺序
总结
1、父类静态变量和静态代码块(先声明的先执行);
2、子类静态变量和静态代码块(先声明的先执行);
3、父类的变量和代码块(先声明的先执行);
4、父类的构造方法;
5、子类的变量和代码块(先声明的先执行);
6、子类的构造方法。
注意事项
1、非静态代码块于构造方法之前执行,并且每次实例化对象之前都会先执行非静态代码块,静态代码块于非静态代码块之前执行;
2、在类第一次被调用时,加载该类,静态代码块只执行一次;
3、静态代码块只能调用静态变量,静态方法只能调用静态变量;
4、非静态代码块或非静态方法可以调用任何(静态+非静态)变量;
5、非静态代码块在实例化对象时,于构造方法之前执行。
参考代码 1
class HelloA {
public HelloA() {
System.out.println("HelloA");
}
{
System.out.println("I'm A class");
}
static {
System.out.println("static A");
}
}
public class HelloB extends HelloA {
public HelloB() {
System.out.println("HelloB");
}
{
System.out.println("I'm B class");
}
static {
System.out.println("static B");
}
public static void main(String[] args) {
new HelloB();
}
}
运行结果
static A
static B
I'm A class
HelloA
I'm B class
HelloB
```
参考代码 2
class A {
public A() {
System.out.println("HelloA");
}
{
System.out.println("I'm A class");
}
static {
System.out.println("static A");
}
}
public class B extends A {
public B() {
System.out.println("HelloB");
}
{
System.out.println("I'm B class");
}
static {
System.out.println("static B");
}
public static void main(String[] args) {
new B();
}
}
运行结果
static A
static B
I'm A class
HelloA
I'm B class
HelloB
```
参考代码 3
class A {
static {
System.out.print("1");
}
public A() {
System.out.print("2");
}
}
class B extends A {
static {
System.out.print("a");
}
public B() {
System.out.print("b");
}
}
public class Hello {
public static void main(String[] args) {
new B();
new B();
}
}
运行结果
1a2b2b
```
参考代码 4
public class HelloA {
static {
System.out.println("static A");
}
{
System.out.println("I'm A class");
}
public HelloA() {
System.out.println("HelloA");
}
public HelloA(String s) {
System.out.println(s + "HelloA");
}
public static void main(String[] args) {
new HelloB();
}
}
class HelloB extends HelloA {
public HelloB() {
// 只能调用一次父类的构造方法,如果不写,Java 编译器会自动调用无参的构造方法。
// super();
super("parent");
System.out.println("HelloB");
}
{
System.out.println("I'm B class");
}
static {
System.out.println("static B");
}
}
运行结果
static A
static B
I'm A class
parentHelloA
I'm B class
HelloB
```
参考代码 5
public class People {
String name;
public People() {
System.out.println(1);
}
public People(String name) {
System.out.println(2);
this.name = name;
}
}
class Child extends People {
People father;
public Child() {
// super(); // 系统默认调用父类的无参构造方法
System.out.println(4);
}
public Child(String name) {
// super(); // 系统默认调用父类的无参构造方法
System.out.println(3);
this.name = name;
father = new People(name + ":F");
System.out.println(father.name);
}
public static void main(String[] args) {
new Child();
// new Child("mike");
}
}
运行结果
1
4
```
参考代码 6
public class People {
String name;
public People() {
System.out.println(1);
}
public People(String name) {
System.out.println(2);
this.name = name;
}
}
class Child extends People {
People father;
public Child() {
// super(); // 系统默认调用父类的无参构造方法
System.out.println(4);
}
public Child(String name) {
// super(); // 系统默认调用父类的无参构造方法
System.out.println(3);
this.name = name;
father = new People(name + ":F");
System.out.println(father.name);
}
public static void main(String[] args) {
// new Child();
new Child("mike");
}
}
运行结果
1
3
2
mike:F
```
参考代码 7
public class ExA {
private static ExA a = new ExA();
static {
System.out.println("父类---静态代码块");
}
public ExA() {
System.out.println("父类---构造方法");
}
{
System.out.println("父类---非静态代码块");
}
public static void main(String[] args) {
new ExB();
}
}
class ExB extends ExA {
private static ExB b = new ExB();
static {
System.out.println("子类---静态代码块");
}
{
System.out.println("子类---非静态代码块");
}
public ExB() {
System.out.println("子类---构造方法");
}
}
运行结果
父类---非静态代码块
父类---构造方法
父类---静态代码块
父类---非静态代码块
父类---构造方法
子类---非静态代码块
子类---构造方法
子类---静态代码块
父类---非静态代码块
父类---构造方法
子类---非静态代码块
子类---构造方法
```
参考代码 8
class Foo {
public Foo(String word) {
System.out.println(word);
}
}
class Parent {
static Foo FOO = new Foo("Parent's static parameter");
Foo foo = new Foo("Parent's parameter");
static {
System.out.println("Parent's static code block");
}
{
System.out.println("Parent's code block");
}
public Parent() {
System.out.println("Parent.Parent()");
}
}
public class Child extends Parent {
static Foo FOO = new Foo("Child's static parameter");
Foo foo = new Foo("Child's parameter");
static {
System.out.println("Child's static code block");
}
{
System.out.println("Child's code block");
}
public Child() {
System.out.println("Child.Child()");
}
public static void main(String[] args) {
new Child();
}
}
运行结果
Parent's static parameter
Parent's static code block
Child's static parameter
Child's static code block
Parent's parameter
Parent's code block
Parent.Parent()
Child's parameter
Child's code block
Child.Child()
```
参考代码 9
public class GFG {
public GFG(int x) {
System.out.println("ONE argument constructor");
}
public GFG() {
System.out.println("No argument constructor");
}
static {
System.out.println("1st static init");
}
{
System.out.println("1st instance init");
}
{
System.out.println("2nd instance init");
}
static {
System.out.println("2nd static init");
}
public static void main(String[] args) {
new GFG();
new GFG(8);
}
}
运行结果
1st static init
2nd static init
1st instance init
2nd instance init
No argument constructor
1st instance init
2nd instance init
ONE argument constructor
```
参考代码 10
class MyTest {
static {
initialize();
}
private static int sum;
public static int getSum() {
initialize();
return sum;
}
private static boolean initialized = false;
private static void initialize() {
if (!initialized) {
for (int i = 0; i < 100; i++)
sum += i;
initialized = true;
}
}
}
public class GFG {
public static void main(String[] args) {
System.out.println(MyTest.getSum());
}
}
运行结果
9900
```
参考代码 11
class MyTest {
static {
initialize();
}
private static int sum = 0;
public static int getSum() {
initialize();
return sum;
}
private static boolean initialized = false;
private static void initialize() {
if (!initialized) {
for (int i = 0; i < 100; i++)
sum += i;
initialized = true;
}
}
}
public class GFG {
public static void main(String[] args) {
System.out.println(MyTest.getSum());
}
}
运行结果
4950
```
参考代码 12
class MyTest {
static {
initialize();
}
private static int sum = 0;
public static int getSum() {
initialize();
return sum;
}
private static boolean initialized;
private static void initialize() {
if (!initialized) {
for (int i = 0; i < 100; i++)
sum += i;
initialized = true;
}
}
}
public class GFG {
public static void main(String[] args) {
System.out.println(MyTest.getSum());
}
}
运行结果
0
```
参考代码 13
class MyTest {
static {
initialize();
}
private static int sum;
public static int getSum() {
initialize();
return sum;
}
private static boolean initialized;
private static void initialize() {
if (!initialized) {
for (int i = 0; i < 100; i++)
sum += i;
initialized = true;
}
}
}
public class GFG {
public static void main(String[] args) {
System.out.println(MyTest.getSum());
}
}
运行结果
4950
```
10-13 题解析
解析:[https://www.cnblogs.com/javaee6/p/3714716.html](https://www.cnblogs.com/javaee6/p/3714716.html)
参考资料
- https://www.geeksforgeeks.org/order-execution-initialization-blocks-constructors-java/
- https://www.cnblogs.com/jj-chenjunjie/p/5331107.html
- https://www.cnblogs.com/javaee6/p/3714716.html
Java(静态)变量、(静态)代码块、构造方法的执行顺序的更多相关文章
- java学习(一)静态代码块 构造代码块 构造方法的执行顺序及注意问题
今天我总结了一下java中静态代码块 构造代码块 构造方法的执行顺序及其注意问题 首先要知道静态代码块是随着类的加载而加载,而构造代码块和构造方法都是随着对象的创建而加载 当时做了这么一个小案例(想必 ...
- Java面试题 静态代码块 构造代码块 构造方法 的执行顺序
JAVA中的静态代码块 构造代码块 构造方法执行顺序: 静态代码块(类加载时执行)>>构造代码块>>构造方法 下面展示一个简单的例子,推荐大家动手运行一遍: public cl ...
- java:构造方法:无参构造/有参构造 this static关键字 静态变量 静态方法 代码块 封装 静态常量。
/*构造方法是一种特殊的方法,专门用于构造/实例化对象,形式:[修饰符] 类名(){ }构造方法根据是否有参数分为无参构造和有参构造*/public class Dog { ...
- Java静态代码块、代码块及构造函数执行顺序
根据以下程序进行分析 定义一个父类 package sas.LearnJava; public class ExcuteOrderTest { { System.out.println("我 ...
- java中成员变量、代码块、构造函数运行顺序
1.java虚拟机执行程序,首先须要装载类,安装现装载父类,初始化父类的静态代码块和静态成员变量 再load子类. 初始化子类静态代码块和成员变量 2.load完成父类与子类后,从main函数入口运行 ...
- Java面向对象理解_代码块_继承_多态_抽象_接口
面线对象: /* 成员变量和局部变量的区别? A:在类中的位置不同 成员变量:在类中方法外 局部变量:在方法定义中或者方法声明上 B:在内存中的位置不同 成员变量:在堆内存 局部变量:在栈内存 C:生 ...
- java类成员变量与代码块初始化
首先根据下面的这个一段代码:引入关于java初始化顺序的问题public class InitationTest extends Person { public InitationTest() { S ...
- 【Java基础】继承中的代码块和构造方法的执行顺序探索
本文讲述有关一个类的静态代码块,构造代码块,构造方法的执行流程问题.首先来看一个例子 /** * Created by lili on 15/10/19. */ class Person{ stati ...
- No.4.测试子类继承父类各代码块和构造方法的执行顺序
Son子类 public class Son extends Parent { static String y ="son的static属性"; public static voi ...
随机推荐
- ubuntu 16.04 镜像下载
下载地址: http://mirror.pnl.gov/releases/xenial/ Ubuntu 14.04.5 LTS (Trusty Tahr)http://releases.ubuntu. ...
- jquery ajax thinkphp异步局部刷新完整流程
环境:ThinkPHP3.2.3,jQuery3.2 前言: 在一般的网站中,都需要用到jquery或者其他框架(比如angular)来处理前后端数据交互,thinkphp在后台也内置了一些函数用 ...
- 2017 Multi-University Training Contest - Team 1
1006(6038) 就是对a,b分别求循环节,先统计一下b中所有长度循环节的出现次数,再对a求循环节时只要满足: a的循环节长度 % b的循环节长度=0,那么这个b的循环节就可以计入答案,尼玛只要是 ...
- CF760 C. Pavel and barbecue 简单DFS
LINK 题意:给出n个数,\(a_i\)代表下一步会移动到第\(a_i\)个位置,并继续进行操作,\(b_i\)1代表进行一次翻面操作,要求不管以哪个位置上开始,最后都能满足 1.到达过所有位置 2 ...
- 谨慎重载clone方法
本文涉及到的概念 1.浅拷贝和深拷贝 2..clone方法的作用和使用方式 3.拷贝构造器和拷贝工厂 1.浅拷贝和深拷贝 浅拷贝 一个类实现Cloneable接口,然后,该类的实例调用clone方 ...
- Java并发编程学习路线
一年前由于工作需要从微软技术栈入坑Java,并陆陆续续做了一个Java后台项目,目前在搞Scala+Java混合的后台开发,一直觉得并发编程是所有后台工程师的基本功,所以也学习了小一年Java的并发工 ...
- 【leetcode 简单】第二题 反转整数
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假 ...
- 基于bootstrap物资管理系统后台模板——后台
链接:http://pan.baidu.com/s/1geKwVMN 密码:0utl
- (1)剑指Offer之斐波那契数列问题和跳台阶问题
一 斐波那契数列 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 问题分析: 可以肯定的是这一题通过递归的方式是肯定能做出来,但是这样会有 ...
- 转: oracle中schema指的是什么?
看来有的人还是对schema的真正含义不太理解,现在我再次整理了一下,希望对大家有所帮助. 我们先来看一下他们的定义:A schema is a collection of database obje ...