PackageInnerclassdemo;/*** Java member inner class * * The 1.Inner class is defined inside the outer class, equivalent to the position of a member variable of the outer class, and the inner class can use any access control such as public, private, protected * 2. The test () method defined in the inner class can directly access data in the outer class without being affected by the access modifier, such as direct access to the private property in the outer class, A * 3. After you define a member inner class, you must use an external class object to create an inner class object. Instead of just going to new an inner class object, that is, the inner class object name = Outer class object. New inner class (); * 4. After compiling the above program, it is found that two. class files are generated *@authorGX * NOTE: * External classes cannot directly use the member methods in an inner class, you can create an inner class object, and then access the member variables in the inner class through the object of the internal classes * External classes and inner classes have the same member variables or methods, and internal classes access their member variables or methods by default, if you want to access the members of the outer Can use the This keyword **///External class outer Public classOuter {Private inta=99; Public classinner{intb =2; Public voidTest () {System.out.println ("Access a: In an external class" +outer. This. a); System.out.println ("Access B in inner class:" +b); } } //Test member Inner class Public Static voidMain (String[]args) {Outer o=NewOuter (); Inner I=o.NewInner (); I.test (); }}
Java Internal classes