java初始化的面试题

java初始化的面试题

1、类的初始化顺序

  1. 初始化父类的静态成员
  2. 初始化父类的静态代码块
  3. 初始化子类的静态成员
  4. 初始化子类的静态代码块
  5. 初始化父类的非静态成员
  6. 初始化父类的非静态代码块
  7. 初始化父类的构造方法
  8. 初始化子类的非静态成员
  9. 初始化子类的非静态代码块
  10. 初始化子类的构造方法

题目1: https://www.jianshu.com/p/e431757bc3d8

package com.tom.tom.jul.shilihua;

public class javaExtends {
    public  int test = this.test();
    public static int method = method();

    static{
        System.out.print("11 ");
    }
    javaExtends(){
        System.out.print("12 ");
    }
    {
        System.out.print("13 ");
    }
    public int test(){
        System.out.print("14 ");
        return 1;
    }

    public static int method(){
        System.out.print("15 ");
        return 1;
    }
    public static void main(String[] args) {
        Son s = new Son();
    }
}

class Father{
    public  int test = test();
    public static int method = method();

    static{
        System.out.print("1 ");
    }
    Father(){
        System.out.print("2 ");
    }
    {
        System.out.print("3 ");
    }
    public int test(){
        System.out.print("4 ");
        return 1;
    }

    public static int method(){
        System.out.print("5 ");
        return 1;
    }
}

class Son extends Father{
    public  int test = test();
    public static int method = method();

    static{
        System.out.print("6 ");
    }
    Son(){
        System.out.print("7 ");
    }
    {
        System.out.print("8 ");
    }
    public int test(){
        System.out.print("9 ");
        return 1;
    }

    public static int method(){
        System.out.print("10 ");
        return 1;
    }
}

输出:15 11 5 1 10 6 9 3 2 9 8 7

题目2: https://www.cnblogs.com/atom-wangzh/p/8718323.html

package com.tom.tom.jul;

public class ShiLiHua2 {
    public static int k = 0;
    public static ShiLiHua2 t1 = new ShiLiHua2("t1");
    public static ShiLiHua2 t2 = new ShiLiHua2("t1");
    public static int i = print("i");
    public static int n = 99;
    public int j = print("j");

    static {
        print("静态代码块");
    }

    {
        print("构造块");
    }

    public static int print(String str){
        System.out.println((++k) + ": " + str + " i=" + i + " n=" + n);
        ++n;
        return ++i;
    }

    public ShiLiHua2(String str) {
        System.out.println((++k) + ": " + str + " i=" + i + " n=" + n);
        ++i;
        ++n;
    }


    public static void main(String[] args) {

        ShiLiHua2  shiLiHua2 = new ShiLiHua2("init");
    }
}

输出:

1: j i=0 n=0
2: 构造块 i=1 n=1
3: t1 i=2 n=2
4: j i=3 n=3
5: 构造块 i=4 n=4
6: t1 i=5 n=5
7: i i=6 n=6
8: 静态代码块 i=7 n=99
9: j i=8 n=100
10: 构造块 i=9 n=101
11: init i=10 n=102

题目三:

package com.tom.tom.jul;

public class ShiLihua {
    private static final ShiLihua shiLihua = new ShiLihua();
    private static int i;
    private static int j = 0;
    //private static final ShiLihua shiLihua = new ShiLihua();
    static {
        System.out.print("静态代码块 ");
        System.out.print(i + " ");
        System.out.print(j);
        System.out.println();
    }

    {
        System.out.print("构造快 ");
        System.out.print(ShiLihua.i + " ");
        System.out.print(ShiLihua.j);
        System.out.println();
    }

    private ShiLihua() {
        ++i;
        ++j;
        System.out.print("构造函数 ");
        System.out.print(i + " ");
        System.out.print(j);
        System.out.println();
    }

    public static ShiLihua getShiLihua() {
        return shiLihua;
    }

    public static void showIAndJ(){
        System.out.print(i + " ");
        System.out.print(j);
    }

    public static void main(String[] args) {
        //ShiLihua.getShiLihua();
        //ShiLihua.showIAndJ();
        //System.out.println("sd");
    }
}

输出:

构造快 0 0
构造函数 1 1
静态代码块 1 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值