In reading Java some of the data structure source code, often find the use of member internal classes, such as node and TreeNode in HashMap, so we will summarize it.
1. member Inner class
(1) Internal and external access rights
Constructs an outer class and an inner class
Public class Out { private String str= "outstr"; Private void print () { System.out.println (str); } Private class in{ public String instr= "InStr"; Public void Test () { System.out.println (str); Print (); }}}
Test
Public Static void Main (string[] args) { out-out =new out (); Out.inin =out. New In (); In.test (); }
The result shows that the inner class can arbitrarily access the member function member variables of the outer class, and the outer class cannot freely access the members of the inner class.
In addition, the inner class can have modifiers, such as private, but it succeeded.
(2) will the inner class be initialized when the external class is initialized?
Public class Out { public out () { System.out.println ("Out"); } Public class in{ public in () { System.out.println ("in"); } } publicstaticvoid main (string[] args) {out-out =New out (); }}
Well, obviously not.
(3) How to deal with internal and external method name conflicts
Public classOut { Public voidTest () {System.out.println ("Out"); } Public classin{ Public voidTest () {System.out.println ("In"); } } Public Static voidMain (string[] args) {out-out=Newout (); Out.in in=out.NewIn (); In.test (); }}
Apparently calling the inner class itself, the method of the outer class is hidden away
2. Static member Inner class
(1) Internal and external access rights
Public classOut {PrivateString str= "Outstr"; Private StaticString static_str= "Static_str"; Public Static classin{ Public voidTest () {System.out.println (STATIC_STR); } } Public Static voidMain (string[] args) {//out out=new out ();In in=NewIn (); In.test (); }}
The result is that the inner class can now invoke only the static members of the outer class, and the initialization of the inner class is no longer entirely dependent on the outer class.
(2) will the inner class be initialized when the external class is initialized?
Public class Out { public out () { System.out.println ("Out"); } Public Static class in{ public in () { System.out.println ("in"); } } publicstaticvoid main (string[] args) {out-out =New out (); }}
Or not?
Java member Inner class