简体   繁体   English

java 与 groovy 中的 Static 块和变量初始化顺序

[英]Static block and variables initialization order in java vs groovy

class Test {
      static void main(String... args) {
      StaticExample se = new StaticExample()
      }

} }

And in another class as below:而在另一个 class 如下:

class StaticExample {
    static {
        add();
   }
   
   static int x=90;
    private static void add() {
        System.out.println("-------------"+x);
    }
}

I have a scenario similar to the above.我有一个类似于上面的场景。

I understand that in Java, static fields are initialized in the same order they are written.我知道在 Java 中,static 字段的初始化顺序与它们写入的顺序相同。 So if the static block is first, I get output as x=0 and if the initialization of x=90 is kept first, I get the output for x as 90. But this is not happening in groovy. So if the static block is first, I get output as x=0 and if the initialization of x=90 is kept first, I get the output for x as 90. But this is not happening in groovy. Whatever order, I always get x=90.无论顺序如何,我总是得到 x=90。 Can someone please clarify this.有人可以澄清一下吗。 Also please tell me what should be done if I want the static block to be initialized first in groovy.另外请告诉我如果我想首先在 groovy 中初始化 static 块应该怎么做。

Looks like Groovy allows forward referencing of a variable in static block and because of that there is a difference in behavior.看起来 Groovy 允许前向引用 static 块中的变量,因此存在行为差异。 If you want to achieve the same by deferring the declaration and initialization of variable.如果你想通过推迟变量的声明和初始化来达到同样的效果。 Please refer to my experimental code in Java and Groovy to get an idea.请参考我在 Java 和 Groovy 中的实验代码来了解一下。


StaticExample.groovy静态Example.groovy

class StaticExample {
    static {
        method()
    }

    static int b = a

    static {
        println("Before Initializing a --- a = $a")
    }

    static {
        println("Before Initializing a --- b = $b")
    }

    static int a = 10

    static {
        println("After Initializing a --- a = $a")
    }

    static {
        println("After Initializing a --- b = $b")
    }

    static int x

    static {
        x = 90
        println("Setting x = 90")
    }

    private static void method() {
        println("Inside method $x")
    }
}

StaticExample.java StaticExample.java

class StaticExample {
    static {
        method();
    }

    static int b;

    static {
        b = getA();
    }

    private static int getA() {
        return a;
    }

    static {
        System.out.println("Before Initializing a --- a = NOT POSSIBLE");
    }

    static {
        System.out.println("Before Initializing a --- b = " + b);
    }

    static int a = 10;

    static {
        System.out.println("After Initializing a --- a = " + a);
    }

    static {
        System.out.println("After Initializing a --- b = " + b);
    }

    static int x;

    static {
        x = 90;
        System.out.println("Setting x = 90");
    }

    private static void method() {
        System.out.println("Inside method x = " + x);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM