java - 语法 - Java 类的静态段

标签 java static

我一直在调试一些项目,并且在短短几天内我多次遇到过这种代码语法。我以前从未见过这种情况,搜索也没有帮助。这段代码的作用是什么?

public class SomeClass{
    // Class member declarations...

    static{
        // Code / method calls. Basically looks like a method.
        // What is this?
    }

    // method declarations...
}

最佳答案

这是一个静态初始化 block 。

更多信息请点击:http://docs.oracle.com/javase/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.

它们在类加载时被调用。

关于java - 语法 - Java 类的静态段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25207744/

相关文章:

java - 注释类型 Transactional 的属性 readOnly 未定义

java邮件轮询从邮件中读取内联或嵌入图像(笑脸)

c# - 如何区分同名的局部变量和静态变量

c++ - C++查询类的静态成员变量

java - 数组值未按预期工作

java - Apache Forrest 作为代码文档解决方案

java - 餐厅账单,初始化错误

java - Java 中的静态和非静态 vs 泛型和通配符

c# - 如何在 C# 中创建具有静态工厂方法的基类?

java - 静态ArrayList总是替换旧值