The function of an inner class is to access private data in an external class
member Inner class:
A member's inner class can be equated as a member variable.
Member inner classes cannot have static declarations.
The member inner class can access all the data for the external class.
public class outerclass{//static variable private static string s1 = "A";//member Variable private String s2 = "B";//static Method Private static void m1 () {System.out.println ("static" S m1 method execute! ");} Member Method private void m2 () {System.out.println ("m2 method execute!");} The member inner class//can be decorated with the modifiers of access control permissions. Public,protected,private, default class innerclass{//static method//public static void m3 () {}// Member Method Public void m4 () {System.out.println (S1); M1 (); SYSTEM.OUT.PRINTLN (s2); M2 ();}} Entry Public static void main (String[] args) {//Create external class object Outerclass oc = new outerclass (); Innerclass inner = oc.new innerclass ();//Note the access method of the member's inner Class INNER.M4 ();}} Static inner class: can be regarded as static variable  , can access static data of external class, cannot access member directly. public class outerclass{//static variable private static string s1 = "A";//member Variable private String s2 = "B";//static method private static void M1 () {System.out.println ("static ' s m1 method execute!");} Member Method private void m2 () {System.out.println ("m2 method execute!");} Static inner classes//can be decorated with modifiers of access control permissions. Public,protected,private, default static class innerclass{//static method Public static void m3 () { System.out.println (S1); M1 ();//system.out.println (S2);//m2 ();} Member Method Public void m4 () {System.out.println (S1); M1 ();//system.out.println (S2);//m2 ();// You cannot access member methods and member variables for external classes. }}//entrance Public static void main (String[] args) {//execute m3OuterClass.InnerClass.m3 ();// Execute M4innerclass inner = new outerclass.innerclass ();//create object of static inner class INNER.M4 ();}} Local inner classes: local inner classes equivalent to local variables, and local inner classes must be final decorated; when accessing local variables (the new features in jdk1.8 appear to be canceled). public class outerclass{//Method Public void m1 () {//local variable final int i Why did = 10;//use final retouching? Local inner class//local inner classes cannot be decorated with access control permission modifiers. class innerclass{//inner class cannot have static declaration//public static void m1 () {}//member Method publiC void m2 () {System.out.println (i); // 10}}//call M2innerclass inner = new innerclass (); inner.m2 ();//How to use the M2 method. }//entrance Public static void main (String[] args) {outerclass oc = new Outerclass (); Oc.m1 ();} Anonymous inner class:/* Anonymous inner class: refers to a class without a name. */public class test{//static method Public static void t (CustomerService cs) {cs.logout ();} Entry Public static void main (String[] args) {//Call T Method//t (New customerserviceimpl ());// Execute the T method using an anonymous inner class//the entire "New customerservice () {}" is an anonymous inner class T (New customerservice () {public void Logout () {System.out.println ("exit!");}); /anonymous inner class advantage: less define a class.//Disadvantage: cannot be reused!}} Interface interface customerservice{//exit system Void logout ();} Write a class to implement the CustomerService interface/*class customerserviceimpl implements customerservice{public Void logout () {System.out.println ("The system has safely exited! ");}} */
This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661202
Inner classes in Java