• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Stack Overflow

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesnt it print anything and causes an overflow

public class ABC{

ABC a= new ABC();

ABC(){

System.out.println("print");

}

public static void main(String s[])
{

ABC a= new ABC();

}}
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

When a class has a member variable like "a", it's initialized before the constructor for that class runs. In this case, "a" is initialized by constructing a new ABC object. The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing another new ABC object. The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object. The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object.The first thing that happens when this ABC is being constructed is its "a" is initialized. Of course, "a" is initialized by constructing a new ABC object....

and eventually, there are so many partially-constructed objects, each one trying to call the constructor of the next and never quite getting there, that the stack overflows.
 
Sree Lakshmi Sugunan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Thanks a Lot. I tried it out and found out the order also:

First control will go to
static variables then
Static block then
Main then
Constructor of the object
assign values to instance variables then
assign values to Init block then
Constructor of the class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic