學(xué)習(xí)是最好的投資!
public class A {
A a = new A();
public static void main(String[] args) {
A a = new A();
}
}
The above code is compiled into bytecode and translated back like this:
public class A {
public A(){
A a = new A();//這里重復(fù)調(diào)用了自己
}
public static void main(String[] args) {
A a = new A();
}
}
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
函數(shù),然后第一次執(zhí)行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.這里就到的問題的關(guān)鍵,成員變量A
初始化還是new A()
繼續(xù)調(diào)用2中的構(gòu)造函數(shù)。
5.接著就會重復(fù)2、3、4的步驟,直到這個線程的??臻g不夠用,拋出StackOverflowError
4. Here comes the key to the problem, member variables AInitialization or continue to call the constructor in 2.
error is thrown. JVM
中,每個線程都會分配一定的??臻g(非共享),這個??臻g可以是固定的可以是動態(tài)擴展的,不同的JVM
StackOverflowError
The reason for the above
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.
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.
public class A
{
public static void main(String[] args)
{
A a = new A();
}
}
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
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.