Читал тут статейку по вложенным классам - решил немного на практике попробовать. Собственно, вопрос, почему после создания объекта внешнего класса, поле вложенного так и остается null, хотя должно было бы получить тоже самое значение что и поле внешнего класса?
public class Airplane {
private String color;
private int id,flight;
public Wing leftWing = new Wing();
public Wing rightWing = new Wing();
Airplane(String color, int id, int flight)
{
this.color = color;
this.id = id;
this.flight = flight;
}
//setters/getters
public int getID()
{
return this.id;
}
public String getColor()
{
return this.color;
}
class Wing
{
private String color;
Wing()
{
this.color = Airplane.this.color;
}
public String getColor()
{
return this.color;
}
}
Main
public static void main(String[] args) {
// write your code here
Airplane plane_1 = new Airplane("red", 343,7642);
System.out.println(plane_1.rightWing.color);
}