Java中的静态块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2943556/
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-13 14:45:11  来源:igfitidea点击:

Static Block in Java

javastatic

提问by Mohit Deshpande

I was looking over some code the other day and I came across:

前几天我正在查看一些代码,我遇到了:

static {
    ...
}

Coming from C++, I had no idea why that was there. Its not an error because the code compiled fine. What is this "static" block of code?

来自 C++,我不知道为什么会在那里。这不是错误,因为代码编译得很好。这个“静态”代码块是什么?

采纳答案by Joachim Sauer

It's a static initializer. It's executed when the class is loaded (or initialized, to be precise, but you usually don't notice the difference).

这是一个静态初始化程序。它在类加载(或初始化,准确地说,但您通常不会注意到差异)时执行。

It can be thought of as a "class constructor".

它可以被认为是一个“类构造函数”

Note that there are also instance initializers, which look the same, except that they don't have the statickeyword. Those are run in addition tothe code in the constructor when a new instance of the object is created.

请注意,还有实例初始化程序,它们看起来相同,只是它们没有static关键字。当创建对象的新实例时,除了构造函数中的代码之外,它们还会运行。

回答by Simon Lehmann

It's a block of code which is executed when the class gets loaded by a classloader. It is meant to do initialization of static members of the class.

它是一个代码块,在类加载器加载类时执行。它旨在对类的静态成员进行初始化。

It is also possible to write non-static initializers, which look even stranger:

也可以编写看起来更奇怪的非静态初始化器:

public class Foo {
    {
        // This code will be executed before every constructor
        // but after the call to super()
    }

    Foo() {

    }
}

回答by aioobe

It is a static initializer. It's executed when the class is loaded and a good place to put initialization of static variables.

它是一个静态初始化器。它在类加载时执行,是放置静态变量初始化的好地方。

From http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html

来自http://java.sun.com/docs/books/tutorial/java/javaOO/initial.html

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

一个类可以有任意数量的静态初始化块,它们可以出现在类体的任何地方。运行时系统保证静态初始化块按照它们在源代码中出现的顺序被调用。

If you have a class with a static look-up map it could look like this

如果您有一个带有静态查找图的类,它可能如下所示

class MyClass {
    static Map<Double, String> labels;
    static {
        labels = new HashMap<Double, String>();
        labels.put(5.5, "five and a half");
        labels.put(7.1, "seven point 1");
    }
    //...
}

It's useful since the above static field could not have been initialized using labels = .... It needs to call the put-method somehow.

这很有用,因为上述静态字段无法使用labels = .... 它需要以某种方式调用 put 方法。

回答by sarath galimelu

Static blocks are used for initializaing the code and will be executed when JVM loads the class.Refer to the below link which gives the detailed explanation. http://www.jusfortechies.com/java/core-java/static-blocks.php

静态块用于初始化代码,将在JVM加载类时执行。请参阅下面的链接,其中给出了详细说明。 http://www.jusfortechies.com/java/core-java/static-blocks.php

回答by Zahid Hussain

A static block executes once in the life cycle of any program, another property of static block is that it executes before the main method.

静态块在任何程序的生命周期中都会执行一次,静态块的另一个特性是它在 main 方法之前执行。

回答by user3777803

Static block can be used to show that a program can run without main function also.

静态块可用于表明程序也可以在没有主函数的情况下运行。

//static block
//static block is used to initlize static data member of the clas at the time of clas loading
//static block is exeuted before the main
class B
{
    static
    {
        System.out.println("Welcome to Java"); 
        System.exit(0); 
    }
}

回答by Piyush

yes, static block is used for initialize the code and it will load at the time JVM start for execution.

是的,静态块用于初始化代码,它将在 JVM 开始执行时加载。

static block is used in previous versions of java but in latest version it doesn't work.

静态块在以前版本的 java 中使用,但在最新版本中它不起作用。