/Users/lyon/j4p/src/bookExamples/ch12Nested/ThisTest.java

1    package bookExamples.ch12Nested; 
2     
3    public class ThisTest { 
4        public static void main(String args[]) { 
5             ThisTest tt; 
6            tt = new ThisTest(); 
7            tt.print(); 
8            tt = new ThisTest() { 
9                public String toString() { 
10                   return "anonymous local inner class from a non-abstract outer class"; 
11               } 
12           }; 
13           tt.print(); 
14           class ThatTest extends ThisTest { 
15               ThisTest anotherTest ; 
16               ThatTest(ThisTest anInstanceOfThisTest) { 
17                   anotherTest = anInstanceOfThisTest; 
18               } 
19               public String toString() { 
20                   return "thatTest is a local named inner class+"+ super.toString() 
21                           + anotherTest.toString(); 
22               } 
23           } 
24           ThatTest tt2 = new ThatTest(tt); 
25           tt2.print(); 
26    
27       } 
28    
29       public void print() { 
30           System.out.println(this); 
31       } 
32    
33       public String toString() { 
34           return "Hello from ThisTest!!"; 
35       } 
36   } 
37    
38   class Constructor { 
39       int x,y; 
40    
41       Constructor(int x, int y) { 
42           this.x = x; 
43           this.y = y; 
44       } 
45    
46       Constructor() { 
47           this(10, 20); // default values 
48       } 
49    
50       void print() { 
51           System.out.println("x,y=" + x + "," + y); 
52       } 
53    
54       public static void main(String args[]) { 
55           Constructor c = new Constructor(); 
56           c.print(); 
57       } 
58   } 
59    
60   class OuterThis { 
61       int x = 10; 
62       Inner i = new Inner(); 
63    
64       public static void main(String args[]) { 
65           OuterThis o = new OuterThis(); 
66           o.i.print(); 
67       } 
68    
69       class Inner { 
70           int x = 20; 
71    
72           void print() { 
73               System.out.println("inner x= " + x); 
74               System.out.println("outer x= " + OuterThis.this.x); 
75           } 
76       } 
77   } 
78    
79