OnJava8-Examples/innerclasses/AnonymousConstructor.java
Bruce Eckel 49edcc8b17 reorg
2015-06-15 17:47:35 -07:00

32 lines
727 B
Java

//: innerclasses/AnonymousConstructor.java
// ©2015 MindView LLC: see Copyright.txt
// Creating a constructor for an anonymous inner class.
import static com.mindviewinc.util.Print.*;
abstract class Base {
public Base(int i) {
print("Base constructor, i = " + i);
}
public abstract void f();
}
public class AnonymousConstructor {
public static Base getBase(int i) {
return new Base(i) {
{ print("Inside instance initializer"); }
@Override
public void f() {
print("In anonymous f()");
}
};
}
public static void main(String[] args) {
Base base = getBase(47);
base.f();
}
} /* Output:
Base constructor, i = 47
Inside instance initializer
In anonymous f()
*///:~