java - Program throws Error


the following program compiles correctly. causing stack overflow error? how exception pushed on stack?

public class reluctant {      private reluctant internalinstance = new reluctant();      public reluctant() throws exception {         throw new exception("i’m not coming out");     }      public static void main(string[] args) {         try {             reluctant b = new reluctant();             system.out.println("surprise!");         } catch (exception ex) {             system.out.println("i told so");         }     } } 

you have field initialization code automatically added constructor body javac compiler. constructor looks this:

private reluctant internalinstance;  public reluctant() throws exception {     internalinstance = new reluctant();     throw new exception("i’m not coming out"); } 

so calls recursively.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -