码迷,mamicode.com
首页 > 编程语言 > 详细

java新建对象的static块与构造器的执行顺序

时间:2018-08-30 12:56:25      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:amp   print   使用   类构造   normal   实例   静态代码块   csu   []   

前言:本文解决的问题

  • 新建一个对象静态代码块什么时候执行
  • {}里面的代码什么时候执行
  • 有继承关系时的执行顺序

1.问题出现的背景:

构造器是用来实例化一个对象,当我们使用new关键字来新建对象时,构造器就会被调用。如果class中含有静态代码块(static)和普通代码块(在{}括号下),新建对象时的调用顺序是:静态代码块>{里面的代码}>构造器

2.例子说明:

2.1代码说明

//父类
public class StaticExample{
     {
        System.out.println("***This is normal block***");
    }
    
    static {
        System.out.println("This is first static block");
    }

    public StaticExample(){
        System.out.println("This is constructor");
    }    
   
    public static String staticString = "Static Variable";

    static {
        System.out.println("This is second static block and "
                                                    + staticString);
    }   

    static {
        staticMethod();
        System.out.println("This is third static block");
    }

    public static void staticMethod() {
        System.out.println("This is static method");
    }

    public static void staticMethod2() {
        System.out.println("This is static method2");
    }
} 

//子类
public class StaticSubClass extends StaticExample{
    static {
        System.out.println("This is the subclass static block");
    }
    {
        System.out.println("***This is the initialise block in subcluss***");
    }
    public StaticSubClass() {
        System.out.println("This is the subclass constructor");
    }
//测试
    public static void main(String[] args){
        StaticSubClass statEx = new StaticSubClass();
        StaticExample.staticMethod2();
    }
}
   

2.1运行结果

This is first static block
This is second static block and Static Variable
This is static method
This is third static block
This is the subclass static block
This is initialise block base
This is base constructor
This is the initialise block in subcluss
This is the subclass constructor
This is static method2

3.结果分析:

3.1static代码块先执行

先分别执行基类的两个静态代码块,然后执行父类的静态代码块,由于父类静态代码块中含有静态方法,因此执行staticMethod(),然后再执行子类的静态代码块,输出为:

This is first static block
This is second static block and Static Variable
This is static method
This is third static block
This is the subclass static block

3.2新建父类,执行初始化代码块

执行完初static代码块后,再执行父类初始化代码块,然后执行父类的构造器,输出为:

This is initialise block base
This is base constructor

3.3新建子类,执行初始化代码块

输出为:

This is the initialise block in subcluss
This is the subclass constructor

3.3执行子类中的方法

最后调用继承父类的方法staticMethod2(),输出:

This is static method2

4.总结

使用new关键字新建对象时,

  • 首先执行static代码块中的,当存在继承关系时,限制性父类的static代码块,然后执行子类的static
  • 其次执行{}初始化代码块,当有继承关系时,执行父类的初始代码块,然后执行父类构造器。
  • 最后执行构造器

5.关于类中static代码块注意事项

  • 不能使用this关键字,因为还没有实例化(You cannot use the this keyword since there is no instance.)
  • 也不能使用super(You shouldn’t try to access super since there is no such a thing for static blocks.)
  • 不要返回值(You should not return anything from this block.)

更多请参考


java新建对象的static块与构造器的执行顺序

标签:amp   print   使用   类构造   normal   实例   静态代码块   csu   []   

原文地址:https://www.cnblogs.com/java-learner/p/9559206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!