私はそれが絶対に退屈な質問であることを知っています(とても初心者です)が、私は立ち往生しています。あるオブジェクトのフィールドに別のオブジェクトからアクセスするにはどうすればよいですか? 質問 => Test02 オブジェクトを 2 回作成しないようにする方法は? (1 回目 => main() ループから、2 回目 => Test01 のコンストラクターから)?
class Main
{
public static void main(String[] args)
{
Test02 test02 = new Test02();
Test01 test01 = new Test01(); //NullPointerException => can't access test02.x from test01
System.out.println(test01.x);
}
}
class Test01
{
int x;
Test02 test02; //can't access this, why? I creat test02 object in main() loop before test01
public Test01()
{
x = test02.x;
}
}
class Test02
{
int x = 10;
}