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
Post a Comment