G
George W. Cherry
Without compiling and running it,
(a) What is the output of InstanceInitBlock.main()?
(b) Explain your answer by commenting the program.
(Note: this is not homework.)
class Base {
protected int a;
protected int b;
void print() {
System.out.println("a: " + a);
}
}
class AnonClassMaker {
Base createAnon() {
return new Base() {
{ a = 5; b = 10; }
void print() {
super.print();
System.out.println("b: " + b);
}
};
}
}
public class InstanceInitBlock {
public static void main(String[] args) {
new AnonClassMaker().createAnon().print();
}
}
(a) What is the output of InstanceInitBlock.main()?
(b) Explain your answer by commenting the program.
(Note: this is not homework.)
class Base {
protected int a;
protected int b;
void print() {
System.out.println("a: " + a);
}
}
class AnonClassMaker {
Base createAnon() {
return new Base() {
{ a = 5; b = 10; }
void print() {
super.print();
System.out.println("b: " + b);
}
};
}
}
public class InstanceInitBlock {
public static void main(String[] args) {
new AnonClassMaker().createAnon().print();
}
}