以下代码来自https://github.com/NanoHttpd/nanohttpd/blob/master/webserver/src/main/java/fi/iki/elonen/SimpleWebServer.java。
代码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 {
是静态初始化程序块。加载类后,该代码将运行一次。LICENSE
设置为text
的值,该值是从bytes.toString()
获得的。
它将在运行时设置。