This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 622
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: 622
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
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2



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: 622
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Anonymous Inner Class Flavour 1 Question
 
Similar Threads
Inner class
Anonymous Inner Class Question
Anonymous Inner Class
Accessing outer class members from instance of inner class
Anonymous Inner class