Internal class is simply put into the inside of a class definition defines another class
Internal divided into categories: internal class members, local inner classes, anonymous inner classes, static inner classes
Members of the inner class: the most common type of internal
public class Outter { private int i = 0; public static String name = "sam"; public Outter(int i) { this.i = i; } class Inner{ public Inner() { System.out.println("Inner Class"); } public void add() { System.out.println(i + " Name: " + name); System.out.println("add()"); } } }
Test categories:
public static void main(String[] args) { Outter outter = new Outter(1); Outter.Inner inner = outter.new Inner(); inner.add(); }
result:
Inner Class 1 Name: sam add()
Members of the inner class Inner Outter appears to be a member of the outer class, so this name
All properties and methods based internal class can access external (including private and static)
External access to internal class is by way of the above-described general category code
When the members of the internal and external class has the same name as the class member variables or methods, hidden phenomenon occurs, the default access inside the case is a member of the class. If you want to access the same name outside the class
Members need to be accessed in the following form:
External class .this. Member variables External class. Member method original: External class .this. Member variables External class .this. Member method
This is not the same as the original and is the access method of the outer class, not this, experimented with jdk1.7 and jdk1.8, I do not know what the original version
public class Outter { private int i = 0; public static String name = "sam"; public Outter(int i) { this.i = i; } private static void del() { System.out.println("Outter del()"); } class Inner{ private int i = 2; public Inner() { System.out.println("Inner Class"); } private void del() { System.out.println("Inner del()"); } public void add() { of the(); Outter.del(); System.out.println("Inner Class: " + i + " Name: " + name); System.out.println("Outter Class: " + Outter.this.i + " Name: " + name); System.out.println("add()"); } } }
Internal class modifier may be arbitrary, but if it is private, accessible only inside the outer class, the rest can not. This is different class and external, outside of class modifiers
The default is public and can only be, because at this time should be equivalent to an internal class members outside of class, and members can be arbitrary
Partial inner class:
The method defined in a class or inside a scope, the difference between it and the internal members of the class that the local access is limited within the class or method within the scope.
Why use a local inner class?
1, implement an interface, you can create and return their role
2, to solve a complex problem, create a class to assist in solving, but do not want this class is available to the public
Not finished. . .
Reference article: Java programming ideas, the Java inner classes Detailed
This blog lot of content from Hazel, be reproduced, also added his own understanding