内部クラスのリフレクションのインスタンス化には、合成パラメーター (外側のクラスのインスタンス) を取るコンストラクターが必要です。内部クラスが静的である場合、そのようなコンストラクターはありません。
クラスがClass.isMemberClass()メソッドを使用して内部クラスであることはわかりますが、メンバー クラスが静的かどうかを判断する適切な方法がわかりません。呼び出すコンストラクタ。
きちんと伝える方法はありますか?
内部クラスのリフレクションのインスタンス化には、合成パラメーター (外側のクラスのインスタンス) を取るコンストラクターが必要です。内部クラスが静的である場合、そのようなコンストラクターはありません。
クラスがClass.isMemberClass()メソッドを使用して内部クラスであることはわかりますが、メンバー クラスが静的かどうかを判断する適切な方法がわかりません。呼び出すコンストラクタ。
きちんと伝える方法はありますか?
デビッドは正しいです。私はちょうどあなたが意味するものを投稿するつもりです
内部クラスのリフレクションのインスタンス化には、合成パラメーター (外側のクラスのインスタンス) を取るコンストラクターが必要です。
それを試す必要がある私のような人々のために:
public class Outer {
public String value = "outer";
public static void main(String[] args) throws Exception {
int modifiers = StaticNested.class.getModifiers();
System.out.println("StaticNested is static: " + Modifier.isStatic(modifiers));
modifiers = Inner.class.getModifiers();
System.out.println("Inner is static: " + Modifier.isStatic(modifiers));
Constructor constructor = Inner.class.getConstructors()[0]; // get the only one
Inner inner = (Inner) constructor.newInstance(new Outer()); // the constructor doesn't actually take arguments
}
public static class StaticNested {
}
public class Inner {
public Inner() {
System.out.println(Outer.this.value);
}
}
}
版画
StaticNested is static: true
Inner is static: false
outer