The inner classes of Java include four types:
Static Member Classes
Member classes
Local classes
Anonymous classes
1. Static member Class
Static member class is a static member of a class that can access only static members and static methods in the upper class
First: When Static member class accesses a member variable or method of outer class, it must first be a new outer object and cannot be accessed by Outer.this.xxx or Outer.this.xxx ().
2. Member class
Similar to the static member class, which is also a member of a class, but it is associated with an instance, so it can access all methods and member variables, even the this pointer of the parent class
The modifiers in front of the class have public > Protected > Package > Private and others are the same.
Note two points in the member's inner class,
First: there can be no static variables and methods in the member's inner class;
Second: the member inner class is dependent on the outer class, so only the perimeter class is created first to be able to create the inner class. So the method to access the inner class in the static method of the outer class is to access the new outer object before the new inner object.
Third: Variables or methods declared in the inner class, if they match the name of the outer class or a function signature, will overwrite the variables or methods of the outer class. Variables or methods of the outer class must be accessed by Outer.this.xxx or Outer.this.xxx () at this time
1 PackageEdu.sjtu.se.dclab.code;2 3 Public classOuter {4 5 PrivateString A;6 PrivateString C;7 Private StaticString B = "Outer static String";8 9 Public Static voidStaticprint () {TenSystem.out.println ("Outer Static Method:staticprint"); One } A - Public voidprint () { -System.out.println ("Outer class Method:print"); the } - - PublicOuter () { -A = "Outer String a"; +c = "Outer String C"; - } + A classinner{ at PrivateString A; - //private static String b= "XX"; Cannot create static variable - - Inner () { -A = "Inner String a"; - } in - Public voidprint () {//method that overrides an external class toSystem.out.println ("Inner class Method:print"); + } - the Public voidTest () { * //Access Outer Member $System.out.println ("OUTER.THIS.C =" + Outer. This. c); accessing members of an external classPanax Notoginseng //Access Outer Member -System.out.println ("c =" +c); Access members of external classes without duplicate names the //Access Outer Static Member +System.out.println ("outer.b =" +outer.b); Accessing static members of an external class A //Access Inner Member theSystem.out.println ("a =" +a); Names, default access to members of inner classes + //Access Outer static method - Staticprint (); static methods for accessing external classes $ //Access Inner Method $ print (); Duplicate names, methods for accessing inner classes - //Access Outer Method -Outer. This. Print (); Duplicate names, methods for accessing external classes the } - }Wuyi the Static classstaticinner{ - Public voidTest () { WuSystem.out.println (NewOuter (). a); In a static inner class, you must first new an object of an outer class - Staticprint (); Static methods for direct access to external classes About } $ } - - PublicInner Getinner () { - return NewInner (); A } + the PublicStaticinner Getstaticinner () { - return NewStaticinner (); $ } the the Public Static voidstatictest () { the NewOuter (). Getinner (). print (); External class static methods to access the methods of the inner class, create an external class object, get the object of the inner class, and then call the method the } - in}
3. Local class
The Local class is a class that is visible only in a code block, similar to a variable defined in a method
Note that the local class can only access the final variable in the code block (the internal reason is not yet understood)
4. Anonymous class
Anonymous class is a local class with No Name
Java Internal Classes