有时为了测试,我使用快速的“双括号”初始化,它在类中创建匿名嵌套类Outer,例如:
static final Set<String> sSet1 = new HashSet<String>() {
{
add("string1");
add("string2");
// ...
}
};
编辑
我正在纠正我之前的错误陈述,即这个示例保持对Outer实例的引用。它没有,它实际上等同于以下内容:
static final Set<String> sSet2;
static {
sSet2 = new HashSet<String>() {
{
add("string1");
add("string2");
// ...
}
};
}
两者都使用匿名嵌套类初始化sSet1,sSet2这些嵌套类不保留对Outer类的引用。
这是否意味着这些匿名类本质上是static nested类?