In another class is defined inside the class called internal type, or nested classes. Class called external package its class.
Internal class action
Package
The public do not want to implement the details of the package to an internal class, inner class can be declared as private, it can only be accessed outside your class.
Provide namespace
Internal static class and outside class can provide namespace is different from the package.
Easy access to external class members
Inner classes can be easily accessed location outside the class members, including private members.
Internal class classification
According to the definition of internal class is assigned a class name can be divided into: anonymous inner classes and inner classes famous.
A scope can be divided into: topical and internal class members have internal class members into inner class: classes and instances internal static inner classes.
Members of the inner class
Similar class member variables outside, inside the inner classes outside the class, and the method defined outside of the body and code blocks.
Examples of internal class
Examples of similar inner class and instance variables can be declared as public / private / protection / default level, while the outer class can be declared public or default level.
public class Outer { public static void main (String [] args) { Outer O = new new Outer (); o.test (); // directly access the instance of the class internal Outer.Inner I = O. new new Inner (); I. the display (); } // external class members Private int X = 10 ; Private void Print () { System.out.println (X); } public void Test () { // access internal class inner i =new new Inner (); i.display (); } // inner class class Inner { // internal class members Private int X =. 5 ; void the display () { // call external class members System.out.println (Outer. the this .x); // call external class member outer. the this .print (); // or Print (); // since only the outer class defines the methods, may be omitted "Outer.this" } } }
Internal class "this" reference is to use the "External class name .this" the current internal class object, you need to reference the outer class object.
Static inner classes
Static inner classes and static variables are similar modification using the keyword "static" when it was declared.
// external class public class View { // external class member variables Private int X = 20 is ; // external static member Private static int StaticX = 10 ; // internal static type static class the Button { void the onClick () { // access external static member System.out.println (StaticX); // compile error, static members can not access non-static member // System.out.println (the X-); } } public static void main (String [] args) { // instantiate static inner class View.Button Button = new new View.Button (); button.onClick (); } }
Partial inner class
Class is defined in the inner local method or block of code within the class, and the scope is limited to the method body code block. Access level is only partially inside the default class, can not be static. Class has access to the local internal All members of the outer class.
public class Outer { Private int value = 10 ; // Final modified parameter, if the parameter is a base type, the parameter can not be changed; if it is a reference type, the reference point parameter can not be changed, but the references to specific values of may be varied public void the Add ( Final int X, int Y) { int Z = 100 ; // local inner class class inner { void the display () { // call external class members int SUM = X + Z + value; the System.out .println (SUM); } } // access the local inner class //= New new Inner Inner Inner (); // inner.display (); // or anonymous objects declared new new Inner () the display ();. } Public static void main (String [] args) { Outer Outer = new new Outer () ; outer.add ( 100, 300 ); } }
Anonymous inner classes
Anonymous inner class is not within the class name, the name is not a local inner class essentially has all the features of a local inner classes. If the anonymous inner class defined in the method, the parameters it visits should be declared "final".