亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

你好,我想問下java中對象的初始化過程具體指什么,是怎樣進行初始化的
PHPz
PHPz 2017-04-18 10:35:57
0
5
429
PHPz
PHPz

學(xué)習(xí)是最好的投資!

reply all(5)
小葫蘆
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不夠用,拋出StackOverflowError4. Here comes the key to the problem, member variables AInitialization 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中,每個線程都會分配一定的??臻g(非共享),這個??臻g可以是固定的可以是動態(tài)擴展的,不同的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. ??
PHPzhong

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template