• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous Inner Class Flavour 1 Question

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,sorry, but this is the code I wanted to post:

Thanks,
Seema
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys...
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit, didn't know that...
Seema
 
reply
    Bookmark Topic Watch Topic
  • New Topic