Non-static nested classes are needed for those cases when a class is created as part of another class (so that many classes with similar names do not create, aka carWheel, bicycleWheel, etc.) (aesthetics) or when a class many uses variables of the outer class (those part). More than they are from ordinary classes are no different (except for the ban on the creation of static variables and static methods).
Based on these data, the question emerges - what do I lose when using composition and aggregation:
class Bicycle{ Wheel wheel; Bicycle(int ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ/*ΠΈ Π΄ΡΡΠ³ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅*/){ wheel = new Wheel(ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ); /*ΠΈ Π΄ΡΡΠ³ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅*/ } Bicycle(/*Π΄ΡΡΠ³ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅*/){ wheel = new Wheel(); /*Π΄ΡΡΠ³ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠ΅*/ } class Wheel{ int ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ; Wheel(int ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ){ this.ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ = ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ; } Wheel(){ ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ=0; } void weGotDamage(){ toStopEveryThing();//ΠΌΡ ΠΈΠΌΠ΅Π΅ΠΌ Π΄ΠΎΡΡΡΠΏ ΠΊ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΌ ΠΈ ΠΌΠ΅ΡΠΎΠ΄Π°ΠΌ Π²Π½Π΅ΡΠ½Π΅Π³ΠΎ ΠΊΠ»Π°ΡΡΠ°! } } void toStopEveryThing(){ //code } public static void main(String[] args){ Bicycle bicycle = new Bicycle(500); bicycle.wheel.weGotDamage();//Π²ΡΠ·ΡΠ²Π°Π΅ΠΌ ΠΌΠ΅ΡΠΎΠ΄ ΠΎΠ±ΡΠ΅ΠΊΡΠ° wheel toStopEveryThing(); } }
Compared to this implementation:
class Bicycle2{ Bicycle2(){} class Wheel{ int ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ; Wheel(int ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ){ this.ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ = ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ; } Wheel(){ ΡΡΠ΅ΠΏΠ΅Π½ΡΠΠ°ΠΊΠ°ΡΠΊΠΈΠΠΎΠ»Π΅Ρ=0; } void weGotDamage(){ toStopEveryThing();//ΠΌΡ ΠΈΠΌΠ΅Π΅ΠΌ Π΄ΠΎΡΡΡΠΏ ΠΊ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΌ ΠΈ ΠΌΠ΅ΡΠΎΠ΄Π°ΠΌ Π²Π½Π΅ΡΠ½Π΅Π³ΠΎ ΠΊΠ»Π°ΡΡΠ°! } } void toStopEveryThing(){ //code } public static void main(String[] args){ Bicycle2 bicycle = new Bicycle2(); Bicycle2.Wheel wheel = bicycle.new Wheel(500); wheel.weGotDamage();//ΠΠ½ΠΎ ΡΠ²ΡΠΆΠ΅ΡΡΡ Ρ bicycle ΠΈ Π²ΡΠ·ΠΎΠ²Π΅Ρ ΠΌΠ΅ΡΠΎΠ΄ toStopEveryThing(); } }