• 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

Stack overFlow error for anonymous class that extends interface

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the following program i have called the anonymous class of dev class.





i am getting stack over flow error as :

Exception in thread "main" java.lang.StackOverflowError
at dev$1.<init>(dev.java:17)
at dev.<init>(dev.java:16)
at dev$1.<init>(dev.java:17)
at dev.<init>(dev.java:16)
at dev$1.<init>(dev.java:17)
at dev.<init>(dev.java:16)
at dev$1.<init>(dev.java:17)
at dev.<init>(dev.java:16)
at dev$1.<init>(dev.java:17)
at dev.<init>(dev.java:16)


can some one tell what is going wrong...is it because the jvm is not able to decide which of the 2 desigs() it has to load in the memory when its object is created in the main..??
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get the error because when a dev instance is created it has to create a new dev instance and store it in e.
This in turn creates new instance which needs to create another instance and so on... Thus stack overflow.

When a subclass is created, the constructor of a base class is called and fields of base class are initialized. No exception for anonymous classes.

When you write your anonymous class implementing emp interface there are no fields to initialize nor any superclass constructor to call (maybe except Object class' constructor).
 
reply
    Bookmark Topic Watch Topic
  • New Topic