I have a question about Anonymous Inner Classes. I am basically trying to create an Anonymous Inner class for Outer class (In Kathy Sierra book, they have created anonymous inner class for some other class and not for outer class). I am pasting the code below. I am getting an exception from the lines which I have commented. Can someone please tell me why am getting the exception when I try to run this program?
I think that the problem is that the constructor is recursively calling itself here. When main() starts, it goes ahead and creates the Outer object,but while creating it, it sees that there is an instance variable 'obj' that's created from an anonymous class(a subclass) of Outer. So the Outer() constructor will be called again which amounts to a StackOverFlowError.
Correct me,if I'm wrong! Seema
Joined: Nov 10, 2007
Maybe you are correct Seema.Can ya tell me how I can call the display method then?
Joined: Apr 29, 2008
Arjun, I really don't know if you can do something like that. Forget anonymous classes, even if I were to create an Object of the same class inside my class, it'd run endlessly. IMHO, I think I'd keep them as two separate classes.
Try what happens when you do this -
I hope you get what I'm trying to say. Can somebody explain this better? Seema