Home > Back-end >  Outside the class methods to create objects, runtime stack overflow exception
Outside the class methods to create objects, runtime stack overflow exception

Time:11-25

The class Circle {
Double redius;
Circle c=new Circle ();


Public static void main (String [] args) {
Circle a=new Circle ();
A.r edius=2.3;
Circle b=new Circle ();
B.r edius=5.5;


System. The out. Println (" a="+ a.r edius +" b="+ b.r edius);
}
}

CodePudding user response:

Create Circle, Circle, create an instance into an infinite loop when they create objects, until out of memory

CodePudding user response:

Circle c=new Circle ();

Is contained in the Circle a Circle c properties

CodePudding user response:

This class is much, can put themselves and play dead

CodePudding user response:

There is a problem? You wrote will stack overflow, of course, if want to hold the same type of object, nor at the time of instantiation is new one.

CodePudding user response:

An infinite loop? Can be interesting or write Circle c=null;

CodePudding user response:

reference 1st floor tianfang response:
create Circle Circle, create an instance into an infinite loop when creating objects, until the memory

Thank you, this may continue to be new, to create objects in the heap memory space, isn't it, why the stack of memory

CodePudding user response:

You new a Circle object is new another Circle objects as members of the first object you, so you become an infinite loop, the last is sure to overflow

CodePudding user response:

refer to 6th floor development reply:
Quote: refer to 1st floor tianfang response:
create Circle Circle, create an instance into an infinite loop when creating objects, until the memory

Thank you, this may continue to be new, to create objects in the heap memory space, isn't it, why to stack memory



Because new is open in the heap, but within the stack will save the variable name, method name, etc., your class is simpler in pile to take up the space is not big, and stack the default size is much smaller than the heap, stack overflow is possible first so,

CodePudding user response:

The class Circle {
Double redius;

Public static void main (String [] args) {
Circle a=new Circle ();
A.r edius=2.3;
Circle b=new Circle ();
B.r edius=5.5;


System. The out. Println (" a="+ a.r edius +" b="+ b.r edius);
}
}
  • Related