class Animal {
public void doMethodOfExtendStaticInnerClass(){}
}
public class Outer {
static Animal a = new Animal(){
public void doMethodOfExtendStaticInnerClass(){
//override me
}
}
public static void main(String[] args) {
Outer.a.doMethodOfExtendStaticInnerClass();
}
}
class Test0{
static class Animal{
String noise;
Animal(String noise){this.noise=noise;}
void makeNoise(){
System.out.println(noise);
};
}
public static void main(String[] args){
//instantiate an anonymous sub-class of Animal with an initialization parameter
new Animal("Woof!"){
//override makeNoise
public void makeNoise(){
for(int i=0; i<3; i++)
super.makeNoise();
}
}.makeNoise();
}
}
but the commentary implies that an anonymous interface implementation cannot have initialization parameters. Here, an anonymous implementation of the CanMakeNoise interface is instantiated.
Not really. Initialization "parameter" would be something that is already defined and you change it by passing a different value. In your example, you are creating a new instance field and an instance initializer with a default value. You are not passing any parameter to the initializer.
If you like our products and services, please help us by posting your review here.
Thought I recalled somewhere it being said that a value assigned inside an init block was regarded as an 'initialization parameter' . Your answer makes more sense.
public class OuterClass {
public class InnerClass {
// Compilation error: Inner class cannot have static declarations.
static final Object field = new Object();
// Same error.
static final void method() {}
}
}