-4

I am a beginner in java. I was trying this code where I created the instance of class in the class itself.

https://i.stack.imgur.com/1IHFB.png

It was giving me stack overflow error. Then, I make the reference variable 'static' in the class and now it is working fine.

enter image description here

My question is what does static keyword do here? I know static variable are class variable which is loaded only once. But, should the constructor call also get into recursive call to itself? I am not able to get any answer from my knowledge about static keyword.

Thelouras
  • 852
  • 1
  • 10
  • 30
  • 5
    Please edit your question to include the code as copy-pasted text instead of images. Also please take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [mcve]. – Some programmer dude Jan 25 '19 at 11:09

2 Answers2

3

You have something like (cannot copy/paste out of screenshots!)

 class A {
     private A a = new A();
 }

So to make an instance of A when you call new A() it has to set the value of this.a by calling new A() again. Infinite recursion (until you run out of stack space).

When you make that field a static, it is no longer initialised when an instance of A is constructed, but only once when the class itself is loaded.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thank you for the answer. But, I still have my doubt. As per java, the static reference variable 'a' gets instantiated at the time of class loading. Then, my question is: Why doesn't it go into infinite recursion at that time like the instance variables? I am asking this because when I run 'System.out.print(D1.obj.obj.d);' ). It is giving me the answer: 19, which, according to me, means that the static reference variable 'obj' contains another reference variable 'obj', much like of infinite recurrsion. – Hanil Kathuria Jan 26 '19 at 14:08
  • It does not go into recursion when calling `new A()` because there are no more recursive calls involved in the constructor. `D1.obj.obj` should also give you a compiler warning because it is misleading to access the static field `obj` through an object instance. `D1.obj` and `D1.obj.obj` (and `D1.obj.obj.obj`) all point to the same instance. – Thilo Jan 27 '19 at 03:13
  • Thank you so much. Although, I have studied about static keyword. But, Can you give me some links so that I can have an in-depth knowledge of static reference variables? – Hanil Kathuria Jan 27 '19 at 09:23
  • Read this: https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class?noredirect=1&lq=1 – Thilo Jan 27 '19 at 09:25
2

When the class is initialized and it has a member of its own type, the member itself is initialized with the same member inside and this member inside the member is initialized... you see the infinite recursion here? Thats the reason why you get a stack overflow error.

The static keyword makes this member not a part of the class. It is initialized once in a global context (e.g. you do not need an instance of D1 to access it. You type D1.obj instead). So the recursion does not happen here.