Is final ill-defined?

前端 未结 6 1692
野的像风
野的像风 2021-01-30 02:39

First, a puzzle: What does the following code print?

public class RecursiveStatic {
    public static void main(String[] args) {
        System.out.println(scale         


        
6条回答
  •  佛祖请我去吃肉
    2021-01-30 03:24

    Not a bug.

    When the first call to scale is called from

    private static final long X = scale(10);
    

    It tries to evaluate return X * value. X has not been assigned a value yet and therefore the default value for a long is used (which is 0).

    So that line of code evaluates to X * 10 i.e. 0 * 10 which is 0.

提交回复
热议问题