Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner Class Flavour 1 Question

 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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?



Exception I got is:
Exception in thread "main" java.lang.StackOverflowError

Thanks.
[ September 19, 2008: Message edited by: Arjun Reddy ]
 
Seema Gaurav
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arjun,
I think this is what is happening:

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
 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you are correct Seema.Can ya tell me how I can call the display method then?

Thanks.
 
Seema Gaurav
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seema Gaurav
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,sorry, but this is the code I wanted to post:

Thanks,
Seema
 
Steve S.
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are stuck in an endless loop. Turn your debugger on and see, the Outer obj.. line gets called over and over again.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


And Seema you can use this button to edit your post if you write something wrong...

[ September 19, 2008: Message edited by: Ankit Garg ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys...
 
Seema Gaurav
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit, didn't know that...
Seema
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic