Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Javaでは、メソッド内でクラス(プログラミング)を定義し、更にインスタンスを作れる
public class HelloWorld {
// 内部クラスとしてRunnableインターフェースを定義
interface MyRunnable {
void run();
}
public static void main(String[] args) {
// MyRunnableインターフェースの匿名クラスを作成
MyRunnable runnable = new MyRunnable() {
@Override
public void run() {
System.out.println("Hello, World!");
}
};
// 実行
runnable.run();
}
}