Detailed examples of internal classes in Java
Detailed examples of internal classes in Java
1: The internal class can directly access the member variables of the external class, including the private member variables of the external class
2: To access the member variables of the internal class, the external class needs to establish the object of the internal class
The internal class can directly access the member variables of the external class because the internal class holds the reference of the external class. Format: external class name this
For example: system out. println("x="+Outer.this.x);// Access X of external class
In inner class (1) and inner class (2), inner classes appear as global variables, that is, they are defined in the class. In this class, inner classes are defined as local variables, that is, they are defined in methods Note in particular: if the internal class wants to access the local variable at this time, the local variable must be modified by final, such as y
If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!