#Trying to create an instance of the abc( ) class within the class itself, what exactly does it do?

35 messages · Page 1 of 1 (latest)

fiery scarab
#
package javaapplication6;

class abc{
    
    abc obj1=new abc();      
    public void method1(){
    System.out.println("Hellworld");
    
    }    
}

public class sample3{
public static void main(String arr[]){
     
   
    }
}

I am trying to create an object of the class abc( ) in the class itself, I am assuming there are no syntactical errors in this code, however what exactly is happening here if I do this ?

ChatGPT Says :

" When you declare abc obj1 = new abc(); directly as an instance variable of the abc class, it creates an infinite loop of object creation. This is because every time you create an instance of abc, it tries to create another instance, and this process repeats indefinitely. "

what does this mean? can someone please explain me the whole process step by step of what's happening in this code, please?

solar kelpBOT
#

This post has been reserved for your question.

Hey @fiery scarab! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

spring siren
#

So, we have a class abc, and it looks like this:

class abc {

}
  • it's empty
#

In reality it's the same as this:

class abc{
  public abc() {
  
  }
}

what you see here is the constructor. Every class has one. If it's not specified, it's just empty, that's why these classes are functionally the same.

#

When you do new abc(); you call that constructor, as if it was a method. But that's not the point here.

class abc {
  Object field = new Object();
}

If a class has a field that's initialized on the same line, that means this field gets initialized when you call the class's constructor

#

So in this case the new Object(); gets called when you call new abc();

#

if you do

class abc {
  abc field = new abc();
}
#

Then it just loops on itself, because you call new abc();, then the constructor calls new abc();, then again and again

#

In practice your application will just freeze or overflow

fiery scarab
#

call within a call and so on..?

spring siren
#

Pretty much. You will create a situation similar to this:

public void someMethod() {
  someMethod();
}
#

if you call someMethod here, it will loop on itself, eventually throwing an exception I think.

humble minnow
#

really all field initializers are put into the default constructor

#

same with static field initializers

#

so what you're writing here is equivalent to ```java
class abc {
abc field;
public abc() {
this.field = new abc();
}
}

#

and as you probably know, calling new <type>() calls the constructor on that <type>

#

which means, the constructor calls itself unconditionally, resulting in what's described above

fiery scarab
humble minnow
#

yeah

fiery scarab
#

ok

#

the default no args constructor of the class abc( ) is instantiating the class abc( ) which is again calling the the default no args constructor of the class abc( )... this repeats until the stack memory is full?

fiery scarab
humble minnow
#

90% right, the constructor makes another new abc instance, which then makes another abc instance, which then makes another abc instance, etc. this repeats until stack memory is full, after which the program crashes.

fiery scarab
fiery scarab
humble minnow
#

it is

fiery scarab
#

ok

#

is it only java or even C/C++ are this complicated??

humble minnow
fiery scarab
#

🙏 May God bless us all

#

Thanks @humble minnow @spring siren

solar kelpBOT