3 回答

慕无忌1623718
TA贡献1744条经验 获得超4个赞
{ // Do Something...}
public class Test { static{ System.out.println("Static"); } { System.out.println("Non-static block"); } public static void main(String[] args) { Test t = new Test(); Test t2 = new Test(); }}
StaticNon-static blockNon-static block

凤凰求蛊
TA贡献1825条经验 获得超4个赞
public class Foo { private static final int widgets; static { int first = Widgets.getFirstCount(); int second = Widgets.getSecondCount(); // Imagine more complex logic here which really used first/second widgets = first + second; }}
first
second
static
public class Foo { private static final int widgets = getWidgets(); static int getWidgets() { int first = Widgets.getFirstCount(); int second = Widgets.getSecondCount(); // Imagine more complex logic here which really used first/second return first + second; }}

小唯快跑啊
TA贡献1863条经验 获得超2个赞
private static final HashMap<String, String> MAP = new HashMap<String, String>(); static { MAP.put("banana", "honey"); MAP.put("peanut butter", "jelly"); MAP.put("rice", "beans"); }
添加回答
举报
0/150
提交
取消