哪个会加载第一个静态变量或静态块? [英] Which will be loaded first static variable or static block?

查看:108
本文介绍了哪个会加载第一个静态变量或静态块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友问我哪个会加载第一个静态变量或静态块。

One of my friends asked me that which will load first static variable or static block.

我的答案指向静态变量。

My answer points to static variable.

所以他给了我两个方程并说要区分它们

b $ b 第一个方程式

So he gave me two equations and said to differentiate between them
First Equation

public class Some {
    public static void main(String args[])
    {
        System.out.println(Some.x);
    }
    static {
        System.out.println(Some.x);
    }
    static int x=90;
}

O / P:0 90

O/P: 0 90

第二个等式

public class Some {
    public static void main(String args[])
    {
        System.out.println(Some.x);
    }
    static int x=90;
    static {
        System.out.println(Some.x);
    }
}

O / P:90 90

O/P: 90 90

我试图对字节码进行反编译,发现它对于上述两个方程都是一样的。
请帮我区分它们。
当静态变量初始化时我很困惑。

I tried to decompile the byte code and found it's same for both the above equation. Please help me to differentiate between them. I am confused when the static variable will initialised.

推荐答案

静态块按它们出现的顺序初始化源文件。
关于堆栈溢出已经有几个问题了...
这个问题对你有好处: Java:初始化静态最终字段的顺序是什么?

Static blocks are initialised in the order they appear in the source file. There are several questions relating to this on stack overflow already... This one has a good answer for you: Java : in what order are static final fields initialized?

这篇关于哪个会加载第一个静态变量或静态块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆