私は内部クラスに新しく、これは非常にばかげた質問かもしれませんが、 "外部クラスのインスタンスなしで内部クラスのインスタンスを作成することはできません"私は私の非外部のクラスのインスタンスメンバーとしてそれを使用して私の非静的な内部クラスのオブジェクトを作成しようとしました。外部クラス参照を参照しない非静的内部クラスオブジェクトの作成
私は外側のクラスオブジェクトへの参照によって内側のオブジェクトを作成していますが、それは正しい方法ですか?以下は
は私のコードスニペットです:
public class TestInner {
private Nonstatic non = null;
private static int access = 4;
public class Nonstatic
{
void hello(){
access = 90;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestInner outer = new TestInner();
TestInner.Nonstatic innern= outer.new Nonstatic();
System.out.println("Non static obj1 is "+innern);
outer.testinnerObj();
}
public void testinnerObj()
{
non = new Nonstatic();
System.out.println("Non static obj2 is "+non);
non.hello();
}
}
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html –