search

Home  >  Q&A  >  body text

你好,我想问下java中对象的初始化过程具体指什么,是怎样进行初始化的

PHPzPHPz2900 days ago415

reply all(5)I'll reply

  • 高洛峰

    高洛峰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();
        }
    }

    reply
    0
  • 大家讲道理

    大家讲道理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的步骤,直到这个线程的栈空间不够用,抛出StackOverflowError4. Here comes the key to the problem, member variables A< /code>Initialization 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. 🎜

    reply
    0
  • PHPz

    PHPz2017-04-18 10:37:57

    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.. .

    reply
    0
  • PHP中文网

    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.

    reply
    0
  • 黄舟

    黄舟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.

    reply
    0
  • Cancelreply