代码是否static {mimeTypes(); ...}
将值传递给 var LICENCE?这是有效的 java 语法吗? var LICENSE 何时会传递值?运行时还是编译时?
/**
* The distribution licence
*/
private static final String LICENCE;
static {
mimeTypes();
String text;
try {
InputStream stream = SimpleWebServer.class.getResourceAsStream("/LICENSE.txt");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = stream.read(buffer)) >= 0) {
bytes.write(buffer, 0, count);
}
text = bytes.toString("UTF-8");
} catch (Exception e) {
text = "unknown";
}
LICENCE = text;
}
最佳答案
static {
是静态初始化 block 。该代码在类加载时运行一次。
LICENSE
设置为 text
的值,该值是从 bytes.toString()
获取的。
它将在运行时设置。
关于java - 这段代码是否向 var 传递了一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831145/