高洛峰2017-04-18 10:37:57
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();//这里重复调用了自己
}
public static void main(String[] args) {
A a = new A();
}
}
大家讲道理2017-04-18 10:37:57
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
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的步骤,直到这个线程的栈空间不够用,抛出StackOverflowError
4. Here comes the key to the problem, member variables A< /code>Initialization or continue to call the constructor in 2.
error is thrown. JVM
中,每个线程都会分配一定的栈空间(非共享),这个栈空间可以是固定的可以是动态扩展的,不同的JVM
StackOverflowError
The reason for the above
PHPz2017-04-18 10:37:57
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.. .
PHP中文网2017-04-18 10:37:57
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.
黄舟2017-04-18 10:37:57
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.