aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Class Flavour 1 Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Class Flavour 1 Question" Watch "Anonymous Inner Class Flavour 1 Question" New topic
Author

Anonymous Inner Class Flavour 1 Question

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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 ]

Be Humble... Be Nice.
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
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

Joined: Nov 10, 2007
Posts: 627
Maybe you are correct Seema.Can ya tell me how I can call the display method then?

Thanks.
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
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

Joined: Apr 29, 2008
Posts: 47
Hey,sorry, but this is the code I wanted to post:

Thanks,
Seema
Steve S.
Greenhorn

Joined: Dec 15, 2005
Posts: 3
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

Joined: Aug 03, 2008
Posts: 9280
    
  17



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

[ September 19, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Thanks Guys...
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
Thanks Ankit, didn't know that...
Seema
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner Class Flavour 1 Question
 
Similar Threads
Inner class
Anonymous Inner class
Anonymous Inner Class
Anonymous Inner Class Question
Accessing outer class members from instance of inner class