• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack overFlow error for anonymous class that extends interface  RSS feed

 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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..??
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!