The simplest answer would be to create an object everynew A().
The following is a slightly detailed analysis of the process 1. After the program is executed, it first enters themainfunction, and then executes it for the first timemain函数,然后第一次执行new A(); 2. Since there is no displayed constructor, the compiler will A no-argument constructor will be generated
public A(){ }
3. After entering the above constructor, you must first initialize the member variables, that is,A a = new A();A a = new A(); 4.这里就到的问题的关键,成员变量A初始化还是new A()继续调用2中的构造函数。 5.接着就会重复2、3、4的步骤,直到这个线程的栈空间不够用,抛出StackOverflowError4. Here comes the key to the problem, member variablesAInitialization or continue to call the constructor in 2.
5. Then steps 2, 3, and 4 will be repeated until the stack space of this thread is not enough and an
error is thrown.JVM中,每个线程都会分配一定的栈空间(非共享),这个栈空间可以是固定的可以是动态扩展的,不同的JVM
We also need to popularize some knowledge about method calling. The implementation methods here can be different. Every time a method is executed in the thread, the method will be pushed into the thread stack in the form of a stack frame.
StackOverflowErrorThe reason for the above
is that the constructor is continuously called, and then stack frames are continuously created and pushed into the thread stack. Finally, the stack space is not enough, causing stack overflow.
Initialization of variables and operation mechanism in memory
Initialization of member variables and operation mechanism in memory
When the system loads a class or creates an instance of the class, the system automatically allocates memory space for member variables, and after allocating memory space, automatically assigns initial values to member variables.
Initialization of local variables and operation mechanism in memory
After local variables are defined, they must be explicitly initialized before they can be used. The system will not initialize local variables. This means that after a local variable is defined, the system does not allocate memory space for the variable. It will not allocate memory for the local variable until the program assigns an initial value to the variable and save the initial value to this memory.
Correct code
public class A { public static void main(String[] args) { A a = new A(); } }
java.lang.StackOverflowError exception
java.lang.StackOverflowError means memory overflow and an infinite loop; the questioner should not create an instance of class A in class A. A() is equivalent to a parameterless constructor. Write A directly in class A a = new A (); will cause infinite iterative creation of instances, eventually leading to memory overflow; the error message also states that the program has an error on line 4, and the error occurs repeatedly
Knowledge supplement
Welcome the questioner to come to my homepage to learn Java knowledge and exchange guidance. The knowledge points mentioned by the questioner are mentioned in the Java Object-Oriented (Part 1) of my homepage: https://segmentfault.com/a/11.. .
The two people above have explained it very clearly. To put it simply, you add an instance variable a of your own to class A. This is not a problem in itself, but you put the initialization of a in the process of instance initialization, so If you call new A() once, you will enter an infinite calling loop, so the stack will overflow.
It is a very obvious recursive call, and there is no end condition, so there will definitely be a memory overflow. Recursion is calling the method itself in the method, and the case you gave is special, it is a constructor method, the reason is that simple.
The above code is compiled into bytecode and translated back like this:
The simplest answer would be to create an object every
new A()
.The following is a slightly detailed analysis of the process
1. After the program is executed, it first enters the
main
function, and then executes it for the first timemain
函数,然后第一次执行new A()
;2. Since there is no displayed constructor, the compiler will A no-argument constructor will be generated
3. After entering the above constructor, you must first initialize the member variables, that is,
5. Then steps 2, 3, and 4 will be repeated until the stack space of this thread is not enough and anA a = new A();
A a = new A();
4.这里就到的问题的关键,成员变量
A
初始化还是new A()
继续调用2中的构造函数。5.接着就会重复2、3、4的步骤,直到这个线程的栈空间不够用,抛出
StackOverflowError
4. Here comes the key to the problem, member variablesAInitialization or continue to call the constructor in 2.
error is thrown.
We also need to popularize some knowledge about method calling. The implementation methods here can be different. Every time a method is executed in the thread, the method will be pushed into the thread stack in the form of a stack frame.JVM
中,每个线程都会分配一定的栈空间(非共享),这个栈空间可以是固定的可以是动态扩展的,不同的JVM
is that the constructor is continuously called, and then stack frames are continuously created and pushed into the thread stack. Finally, the stack space is not enough, causing stack overflow.StackOverflowError
The reason for the aboveInitialization of variables and operation mechanism in memory
Initialization of member variables and operation mechanism in memory
When the system loads a class or creates an instance of the class, the system automatically allocates memory space for member variables, and after allocating memory space, automatically assigns initial values to member variables.
Initialization of local variables and operation mechanism in memory
After local variables are defined, they must be explicitly initialized before they can be used. The system will not initialize local variables. This means that after a local variable is defined, the system does not allocate memory space for the variable. It will not allocate memory for the local variable until the program assigns an initial value to the variable and save the initial value to this memory.
Correct code
java.lang.StackOverflowError exception
java.lang.StackOverflowError means memory overflow and an infinite loop; the questioner should not create an instance of class A in class A. A() is equivalent to a parameterless constructor. Write A directly in class A a = new A (); will cause infinite iterative creation of instances, eventually leading to memory overflow; the error message also states that the program has an error on line 4, and the error occurs repeatedly
Knowledge supplement
Welcome the questioner to come to my homepage to learn Java knowledge and exchange guidance. The knowledge points mentioned by the questioner are mentioned in the Java Object-Oriented (Part 1) of my homepage: https://segmentfault.com/a/11.. .
The two people above have explained it very clearly. To put it simply, you add an instance variable a of your own to class A. This is not a problem in itself, but you put the initialization of a in the process of instance initialization, so If you call new A() once, you will enter an infinite calling loop, so the stack will overflow.
It is a very obvious recursive call, and there is no end condition, so there will definitely be a memory overflow. Recursion is calling the method itself in the method, and the case you gave is special, it is a constructor method, the reason is that simple.