static つき | static なし | 名前 つき | 名前 なし | |
---|---|---|---|---|
メンバー・クラス | static なメンバー・クラス | インナー・クラス | - | - |
ローカル・クラス | - | - | ローカル・インナー・クラス | 匿名インナー・クラス |
- public class Outer { // トップレベル・クラス
- static class Inner1 { ... } // static なメンバー・クラス
- class Inner2 { ... } // メンバー・インナー・クラス
- void m1() {
- class Inner3 { ... } // ローカル・インナー・クラス
- new Thread(new Runnable() { // 匿名インナー・クラス
- public static void run() { ... }
- }).start();
- }
- }
- public class Outer { // トップレベル・クラス
- static void m1() { ... }
- void m2() { ... }
- static class Inner1 { ... } // static なメンバー・クラス
- class Inner2 { ... } // メンバー・インナー・クラス
- static void m3() { // クラス・メソッドから
- m1();
- new Outer().m2();
- Inner1 i11 = new Inner1(); // そのままオブジェクトを生成
- Inner2 i12 = new Outer().new Inner2();
- // トップレベル・クラスのオブジェクトを生成してから
- // メンバー・インナー・クラスのオブジェクトを生成
- }
- void m4() { // インスタンス・メソッドから
- m1();
- m2();
- Inner1 i01 = new Inner1(); // そのままオブジェクトを生成
- Inner2 i02 = new Inner2(); // そのままオブジェクトを生成
- }
- }
- public class MyClass { // 別クラスから
- public static void main(String[] args) {
- Outer.m1();
- new Outer().m2();
- Outer.Inner1 i21 = new Outer.Inner1();
- // トップレベル・クラス名で修飾して、オブジェクトを生成
- Outer.Inner2 i22 = new Outer().new Inner2();
- // トップレベル・クラスのオブジェクトを生成してから
- // メンバー・インナー・クラスのオブジェクトを生成
- }
- }
- public class Outer {
- public int a = 1;
- private int b = 2;
- static int c = 3;
- public void m1(final int d, int e) {
- final int f = 6;
- int g = 7;
- class Inner {
- void m2(final int h, int i) {
- //
- // ここから使える変数: a, b, c ... クラスのフィールド
- // d, f ... final 変数
- // h, i ... 自クラス内の 変数
- }
- }
- }
- public static void main(String[] args) {
- Outer a = new Outer();
- a.m1(4, 5);
- }
- }
new スーパークラス名() { ... } // スーパークラスを継承したサブクラス new インターフェース名() { ... } // インターフェースを実装したクラス