• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Anonymous Classes

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code,



When I compile the above code,
I get the following output.

Inner class display method
Outer class display method

When I make some changes in the code
and looks as below



i guess i would get the same output as I got in the previous case
But, I am getting an error @ runtime (Stack Overflow Error)

Why is it so?
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I explained the problem there link
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When you are creating object of class,all instance variables will be given their default values initially,here in main method when you create object of outer class its instance variable(a) will be given its default value i.e again one more outerclass object will be created, again for this object's instance variable it will try to create outerclass object, like that it will go on and will end up with StackOverflow error.
 
Pradeep Balasubramanian
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Again I am getting StackoverflowError,
when I call by using "new OuterClass()" or "new OuterClass().display()"


I need to get an output without StackOverflowError?

:roll:
[ July 18, 2008: Message edited by: Pradeep Balasubramanian ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as long as anonymous class reference a is there as instance variable you will get the same error when you say new OuterClass()
try this

[ July 18, 2008: Message edited by: ramesh maredu ]
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Balasubramanian:
But Again I am getting StackoverflowError,
when I call by using "new OuterClass()" or "new OuterClass().display()"


I need to get an output without StackOverflowError?

:roll:

[ July 18, 2008: Message edited by: Pradeep Balasubramanian ]



You are getting it when you call new OuterClass(); because each instance of OuterClass has a variable instance initialized with a new OuterClass. So it will be creating the new OuterClass() and your variables instances forever.
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic