All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
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.
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() {}
}
}
"Inner classes can't have static methods though" -> is this still the case in java 17?
It seems to contradict what is written in the explanation to option 1:
Since Java 16, non-static nested class i.e. inner class is allowed to have static members. Before Java 16, they were allowed to have a static field only if that field is final.
class OuterClass {
public class InnerClass {
static int VAL = 10; //COMPILES FINE
static String STR = "1234"; //COMPILES FINE
static Object obj = new Object(); /
static int val2 = 10; //COMPILES FINE
static final void method() {} //COMPILES FINE
}
}
Also, im on java 17, and its seems that inner calsses can have statis members, i.e., fields and methods, it compiles and runs ok
Since Java 16 an inner class is allowed to have static members. That is why option 1 is marked correct. But yes, the statement in the explanation should be fixed.
thank you for your feedback!
If anonymous class is created for interface, it extends Object class and implement that interface, if it is created for a class then it extends that class. Since Java 16, inner classes are allowed to has static fields as well as static methods. Example:
public class Outer
{
class Inner
{
static int k = 10; //valid since Java 16
static void m(); //valid since Java 16
}
}
How can static method m() be valid if it has no body implementation?