KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AnonymousClass


1 /* AnonymousClass Copyright (C) 1999 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: AnonymousClass.java,v 1.7 2000/01/30 16:49:58 jochen Exp $
18  */

19
20 import java.util.Vector JavaDoc;
21
22 public class AnonymousClass {
23 ///#ifndef JAVAC11
24
// javac 1.1 is tooooooo broken
25
class Inner {
26     int var = 3;
27
28     public void test() {
29         final long longVar = 5;
30         final double dblVar = 3;
31
32         class Hello {
33         int var = (int) longVar;
34
35         {
36             System.err.println("all constructors");
37         }
38
39         Hello() {
40             System.err.println("construct");
41         }
42         Hello(String JavaDoc info) {
43             System.err.println("construct: "+info);
44         }
45 ///#ifndef JAVAC12
46
///#ifndef JIKES0
47
Hello(int i) {
48             this("This can only be compiled correctly"
49              +" by a recent jikes");
50         }
51 ///#endif
52
///#endif
53
private void hello() {
54             this.hashCode();
55             Inner.this.hashCode();
56             AnonymousClass.this.hashCode();
57             System.err.println("HelloWorld: "+dblVar);
58         }
59         };
60         final Hello hi = new Hello();
61         final Hello ho = new Hello("ho");
62         final Object JavaDoc o = new Object JavaDoc() {
63         int blah = 5;
64
65         {
66             System.err.println("Anonymous Constructor speaking");
67         }
68
69         Hello hii = hi;
70
71         public String JavaDoc toString() {
72             this.hii.hello();
73             hi.hello();
74             return Integer.toHexString(AnonymousClass.this.hashCode()
75                            +blah);
76         }
77
78         {
79             System.err.println("Anonymous Constructor continues");
80         }
81
82         };
83         Object JavaDoc p = new Object JavaDoc() {
84         public String JavaDoc toString() {
85             return o.toString();
86         }
87         };
88 ///#ifndef JAVAC12
89
Hello blah = new Hello("Hello World") {
90         public void hello() {
91             System.err.println("overwritten" + dblVar + hi);
92         }
93         };
94 ///#endif
95

96         Inner blub = new AnonymousClass().new Inner("Inner param") {
97         public void test() {
98             System.err.println("overwritten");
99         }
100         };
101
102         class Hi extends Inner {
103         public Hi() {
104             super("Hi World");
105         }
106         }
107
108 ///#ifndef JAVAC12
109
class Huhu extends Hello {
110         public Huhu(String JavaDoc str) {
111             super(str);
112         }
113         
114         public Huhu(int i) {
115         }
116
117         public Huhu() {
118             super("What's up");
119         }
120         }
121 ///#endif
122

123         Vector JavaDoc v = new Vector JavaDoc(hi.var, new Inner("blah").var) {
124         public String JavaDoc newMethod() {
125             return super.toString();
126         }
127         };
128
129         Hi hu = new Hi();
130         new Huhu(1);
131         
132     }
133     Inner (String JavaDoc str) {
134     }
135     }
136
137     
138     public void test() {
139     class Hello {
140         int var = 4;
141         
142         Hello() {
143         System.err.println("construct");
144         }
145         Hello(String JavaDoc info) {
146         System.err.println("construct: "+info);
147         }
148         
149         public void hello() {
150         this.hashCode();
151         AnonymousClass.this.hashCode();
152         System.err.println("HelloWorld");
153         }
154     };
155     final Hello hi = new Hello();
156     final Hello ho = new Hello("ho");
157     final Object JavaDoc o = new Object JavaDoc() {
158         int blah = 5;
159         
160         Hello hii = hi;
161         
162         public String JavaDoc toString() {
163         this.hii.hello();
164             hi.hello();
165         return Integer.toHexString(AnonymousClass.this.hashCode()
166                        +blah);
167         }
168     };
169     Object JavaDoc p = new Object JavaDoc() {
170         public String JavaDoc toString() {
171         return o.toString();
172         }
173     };
174 ///#ifndef JAVAC12
175
Hello blah = new Hello("Hello World") {
176         public void hello() {
177         System.err.println("overwritten");
178         }
179     };
180 ///#endif
181

182     Inner blub = new Inner("Inner param") {
183         public void test() {
184             System.err.println("overwritten");
185         }
186     };
187
188     class Hi extends Inner {
189         public Hi() {
190         super("Hi World");
191         }
192     }
193     
194     Vector JavaDoc v = new Vector JavaDoc(hi.var, new Inner("blah").var) {
195         public String JavaDoc newMethod() {
196         return super.toString();
197         }
198     };
199     
200     Hi hu = new Hi();
201     
202     }
203 ///#endif
204
}
205
Popular Tags