Is final ill-defined?

前端 未结 6 1759
野的像风
野的像风 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:09

    It's not a bug at all, simply put it is not an illegal form of forward references, nothing more.

    String x = y;
    String y = "a"; // this will not compile 
    
    
    String x = getIt(); // this will compile, but will be null
    String y = "a";
    
    public String getIt(){
        return y;
    }
    

    It's simply allowed by the Specification.

    To take your example, this is exactly where this matches:

    private static final long X = scale(10) + 3;
    

    You are doing a forward reference to scale that is not illegal in any way as said before, but allows you to get the default value of X. again, this is allowed by the Spec (to be more exact it is not prohibited), so it works just fine

提交回复
热议问题