Sometimes for testing I use quick "double-brace" initialization which creates anonymous nested class in Outer
class, for example:
static final Set<String> sSet1 = new HashSet<String>() {
{
add("string1");
add("string2");
// ...
}
};
Edit
I am correcting my previously faulty statement that this example keeps reference to Outer
instance. It does not and it is effectively equivalent to the following :
static final Set<String> sSet2;
static {
sSet2 = new HashSet<String>() {
{
add("string1");
add("string2");
// ...
}
};
}
both sSet1
and sSet2
are initialized with anonymous nested classes that keep no reference to Outer
class.
Does it mean that these anonymous classes are essentially static nested
classes ?