Java 静态和非静态初始化代码块有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/335311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:34:58  来源:igfitidea点击:

What is the difference between a static and a non-static initialization code block

javastaticstatic-initializer

提问by Szere Dyeri

My question is about one particular usage of static keyword. It is possible to use statickeyword to cover a code block within a class which does not belong to any function. For example following code compiles:

我的问题是关于 static 关键字的一种特殊用法。可以使用static关键字来覆盖不属于任何函数的类中的代码块。例如下面的代码编译:

public class Test {
    private static final int a;    
    static {
        a = 5;
        doSomething(a);
    }
    private static int doSomething(int x) {
        return (x+5);
    }
}

If you remove the statickeyword it complains because the variable ais final. However it is possible to remove both finaland statickeywords and make it compile.

如果您删除static关键字,它会抱怨,因为变量afinal. 但是,可以同时删除finalandstatic关键字并使其编译。

It is confusing for me in both ways. How am I supposed to have a code section that does not belong to any method? How is it possible to invoke it? In general, what is the purpose of this usage? Or better, where can I find documentation about this?

这两种方式都让我感到困惑。我应该如何拥有不属于任何方法的代码部分?怎么可能调用它?一般来说,这种用法的目的是什么?或者更好的是,我在哪里可以找到有关此的文档?

采纳答案by Lawrence Dol

The code block with the static modifier signifies a classinitializer; without the static modifier the code block is an instanceinitializer.

带有 static 修饰符的代码块表示初始值设定项;如果没有 static 修饰符,代码块是一个实例初始值设定项。

Class initializers are executed in the order they are defined (top down, just like simple variable initializers) when the class is loaded (actually, when it's resolved, but that's a technicality).

类初始值设定项按照它们定义的顺序执行(自上而下,就像简单的变量初始值设定项一样)在类加载时(实际上,当它被解析时,但这是一个技术问题)。

Instance initializers are executed in the order defined when the class is instantiated, immediately before the constructor code is executed, immediately after the invocation of the super constructor.

实例初始化器按照类被实例化时定义的顺序执行,紧接在构造函数代码执行之前,紧接在调用超级构造函数之后。

If you remove staticfrom int a, it becomes an instance variable, which you are not able to access from the static initializer block. This will fail to compile with the error "non-static variable a cannot be referenced from a static context".

如果删除staticfrom int a,它将成为一个实例变量,您无法从静态初始化程序块访问该变量。这将无法编译并出现错误“非静态变量 a 无法从静态上下文中引用”。

If you also remove staticfrom the initializer block, it then becomes an instance initializer and so int ais initialized at construction.

如果您也static从初始化程序块中删除,它就会成为一个实例初始化程序,因此int a在构造时被初始化。

回答by Alnitak

The staticblock is a "static initializer".

static块是一个“静态初始化程序”。

It's automatically invoked when the class is loaded, and there's no other way to invoke it (not even via Reflection).

它在类加载时自动调用,并且没有其他方法可以调用它(甚至不能通过反射)。

I've personally only ever used it when writing JNI code:

我个人只在编写 JNI 代码时使用过它:

class JNIGlue {
    static {
        System.loadLibrary("foo");
    }
}

回答by Paul Tomblin

The static code block can be used to instantiate or initialize class variables (as opposed to object variables). So declaring "a" static means that is only one shared by all Test objects, and the static code block initializes "a" only once, when the Test class is first loaded, no matter how many Test objects are created.

静态代码块可用于实例化或初始化类变量(与对象变量相反)。所以声明“a”静态意味着只有一个被所有Test对象共享,并且静态代码块只初始化“a”一次,当Test类第一次加载时,无论创建了多少Test对象。

回答by Vincent Ramdhanie

You will not write code into a static block that needs to be invoked anywhere in your program. If the purpose of the code is to be invoked then you must place it in a method.

您不会将代码写入需要在程序中的任何位置调用的静态块。如果要调用代码的目的,则必须将其放置在方法中。

You can write static initializer blocks to initialize static variables when the class is loaded but this code can be more complex..

您可以编写静态初始化程序块以在加载类时初始化静态变量,但此代码可能更复杂。

A static initializer block looks like a method with no name, no arguments, and no return type. Since you never call it it doesn't need a name. The only time its called is when the virtual machine loads the class.

静态初始化块看起来像一个没有名称、没有参数和没有返回类型的方法。因为你从不叫它它不需要名字。它的唯一调用时间是虚拟机加载类时。

回答by DJClayworth

"final" guarantees that a variable must be initialized before end of object initializer code. Likewise "static final" guarantees that a variable will be initialized by the end of class initialization code. Omitting the "static" from your initialization code turns it into object initialization code; thus your variable no longer satisfies its guarantees.

“final”保证必须在对象初始值设定项代码结束之前初始化变量。同样,“static final”保证在类初始化代码结束时初始化变量。从初始化代码中省略“静态”会将其变成对象初始化代码;因此您的变量不再满足其保证。

回答by cardman

when a developer use an initializer block, the Java Compiler copies the initializer into each constructor of the current class.

当开发人员使用初始化块时,Java 编译器将初始化器复制到当前类的每个构造函数中。

Example:

例子:

the following code:

以下代码:

class MyClass {

    private int myField = 3;
    {
        myField = myField + 2;
        //myField is worth 5 for all instance
    }

    public MyClass() {
        myField = myField * 4;
        //myField is worth 20 for all instance initialized with this construtor
    }

    public MyClass(int _myParam) {
        if (_myParam > 0) {
            myField = myField * 4;
            //myField is worth 20 for all instance initialized with this construtor
            //if _myParam is greater than 0
        } else {
            myField = myField + 5;
            //myField is worth 10 for all instance initialized with this construtor
            //if _myParam is lower than 0 or if _myParam is worth 0
        }
    }

    public void setMyField(int _myField) {
        myField = _myField;
    }


    public int getMyField() {
        return myField;
    }
}

public class MainClass{

    public static void main(String[] args) {
        MyClass myFirstInstance_ = new MyClass();
        System.out.println(myFirstInstance_.getMyField());//20
        MyClass mySecondInstance_ = new MyClass(1);
        System.out.println(mySecondInstance_.getMyField());//20
        MyClass myThirdInstance_ = new MyClass(-1);
        System.out.println(myThirdInstance_.getMyField());//10
    }
}

is equivalent to:

相当于:

class MyClass {

    private int myField = 3;

    public MyClass() {
        myField = myField + 2;
        myField = myField * 4;
        //myField is worth 20 for all instance initialized with this construtor
    }

    public MyClass(int _myParam) {
        myField = myField + 2;
        if (_myParam > 0) {
            myField = myField * 4;
            //myField is worth 20 for all instance initialized with this construtor
            //if _myParam is greater than 0
        } else {
            myField = myField + 5;
            //myField is worth 10 for all instance initialized with this construtor
            //if _myParam is lower than 0 or if _myParam is worth 0
        }
    }

    public void setMyField(int _myField) {
        myField = _myField;
    }


    public int getMyField() {
        return myField;
    }
}

public class MainClass{

    public static void main(String[] args) {
        MyClass myFirstInstance_ = new MyClass();
        System.out.println(myFirstInstance_.getMyField());//20
        MyClass mySecondInstance_ = new MyClass(1);
        System.out.println(mySecondInstance_.getMyField());//20
        MyClass myThirdInstance_ = new MyClass(-1);
        System.out.println(myThirdInstance_.getMyField());//10
    }
}

I hope my example is understood by developers.

我希望我的例子能被开发人员理解。

回答by Madan Sapkota

Uff! what is static initializer?

噗!什么是静态初始化器?

The static initializer is a static {}block of code inside java class, and run only one time before the constructor or main method is called.

静态初始化器是static {}java 类中的一段代码,在调用构造函数或 main 方法之前只运行一次。

OK! Tell me more...

好的!告诉我更多...

  • is a block of code static { ... }inside any java class. and executed by virtual machine when class is called.
  • No returnstatements are supported.
  • No arguments are supported.
  • No thisor superare supported.
  • static { ... }任何 Java 类中的代码块。并在调用类时由虚拟机执行。
  • return支持任何语句。
  • 不支持任何参数。
  • 不支持thissuper支持。

Hmm where can I use it?

嗯,我可以在哪里使用它?

Can be used anywhere you feel ok :) that simple. But I see most of the time it is used when doing database connection, API init, Logging and etc.

可以在任何你觉得不错的地方使用:) 就这么简单。但是我看到大多数时候它是在做数据库连接、API 初始化、日志记录等时使用的。

Don't just bark! where is example?

不要只是吠叫!例子在哪里?

package com.example.learnjava;

import java.util.ArrayList;

public class Fruit {

    static {
        System.out.println("Inside Static Initializer.");

        // fruits array
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Orange");
        fruits.add("Pear");

        // print fruits
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
        System.out.println("End Static Initializer.\n");
    }

    public static void main(String[] args) {
        System.out.println("Inside Main Method.");
    }
}

Output???

输出???

Inside Static Initializer.

Apple

Orange

Pear

End Static Initializer.

Inside Main Method.

静态初始化器内部。

苹果

橘子

结束静态初始化程序。

在 Main 方法中。

Hope this helps!

希望这可以帮助!

回答by Alexei Fando

This is directly from http://www.programcreek.com/2011/10/java-class-instance-initializers/

这直接来自http://www.programcreek.com/2011/10/java-class-instance-initializers/

1. Execution Order

1. 执行顺序

Look at the following class, do you know which one gets executed first?

看下面的类,你知道哪个先执行吗?

public class Foo {

    //instance variable initializer
    String s = "abc";

    //constructor
    public Foo() {
        System.out.println("constructor called");
    }

    //static initializer
    static {
        System.out.println("static initializer called");
    }

    //instance initializer
    {
        System.out.println("instance initializer called");
    }

    public static void main(String[] args) {
        new Foo();
        new Foo();
    }
}

Output:

输出:

static initializer called

instance initializer called

constructor called

instance initializer called

constructor called

静态初始化器调用

调用实例初始值设定项

构造函数调用

调用实例初始值设定项

构造函数调用

2. How do Java instance initializer work?

2. Java 实例初始化器是如何工作的?

The instance initializer above contains a println statement. To understand how it works, we can treat it as a variable assignment statement, e.g., b = 0. This can make it more obvious to understand.

上面的实例初始化器包含一个 println 语句。为了理解它是如何工作的,我们可以把它当作一个变量赋值语句,例如,b = 0。这样可以更容易理解。

Instead of

代替

int b = 0, you could write

int b = 0,你可以写

int b;
b = 0;

Therefore, instance initializers and instance variable initializers are pretty much the same.

因此,实例初始值设定项和实例变量初始值设定项几乎相同。

3. When are instance initializers useful?

3. 实例初始化器什么时候有用?

The use of instance initializers are rare, but still it can be a useful alternative to instance variable initializers if:

实例初始化器的使用很少见,但在以下情况下,它仍然可以作为实例变量初始化器的有用替代:

  1. Initializer code must handle exceptions
  2. Perform calculations that can't be expressed with an instance variable initializer.
  1. 初始化代码必须处理异常
  2. 执行无法用实例变量初始值设定项表示的计算。

Of course, such code could be written in constructors. But if a class had multiple constructors, you would have to repeat the code in each constructor.

当然,这样的代码可以写在构造函数中。但是如果一个类有多个构造函数,你就必须在每个构造函数中重复代码。

With an instance initializer, you can just write the code once, and it will be executed no matter what constructor is used to create the object. (I guess this is just a concept, and it is not used often.)

使用实例初始化程序,您只需编写一次代码,无论使用什么构造函数创建对象,它都会执行。(我猜这只是一个概念,并不经常使用。)

Another case in which instance initializers are useful is anonymous inner classes, which can't declare any constructors at all. (Will this be a good place to place a logging function?)

另一个实例初始化器有用的情况是匿名内部类,它根本不能声明任何构造函数。(这是放置日志功能的好地方吗?)

Thanks to Derhein.

感谢德海因。

Also note that Anonymous classes that implement interfaces [1] have no constructors. Therefore instance initializers are needed to execute any kinds of expressions at construction time.

另请注意,实现接口 [1] 的匿名类没有构造函数。因此,在构造时需要实例初始化器来执行任何类型的表达式。