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