What occurs when object is created in Java?

后端 未结 5 409
情深已故
情深已故 2020-12-08 18:12

My teacher gave me a question:

\"What occurs when objects are created in Java\".

To the best of my knowledge, memory allocation

5条回答
  •  青春惊慌失措
    2020-12-08 18:32

    I'll answer that using a simple example.

    Say you have a class Car. Now you write:

    Car car;
    car = new Car();
    

    The first statement creates a reference with car in the stack.

    In the second statement, the Car class will be loaded to the main memory, then it will allocate memory for the members of Car in the heap. When this happens, the members will be initialized with values provided by the JVM.

提交回复
热议问题