Java tracing confusion -


can me understand why java trying make instance of before b. , also, why looping between line 2 , line 3?

public class winterfell {    private winterfell a= new winterfell();      public winterfell() throws exception {         throw new exception("fire , ice");     }      public static void main(string[] args) {         try {             winterfell b = new winterfell();             system.out.println("surprise!");         } catch (exception ex) {             system.out.println("i told so");         }     } } 

this cause stackoverflowerror.

by having field referencing new object of same class or making new object of same class in constructor have infinite number of calls create new winterfell object.

that why looping.

to fix want remove private winterfell a= new winterfell(); single winterfell object created.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -