MY mENU


Tuesday 15 January 2013

What JVM does internally when an object is created?


When we create object first control is sent to invoked constructor, but its logic will not be executed, instead non-static variables and non-static blocks are executed first in the order they are defined from top to bottom, then after constructor logic is executed.
If all non-static variable and non-static blocks execution is completed, we can say object creation is completed. If constructor execution is completed, we can say object initialization is completed. New key word will initialize the non static variables with default and assigned values. Constructors will reinitialize objects only with developer or given values.

Who will initialize default and assigned values to non-static variables?

new keyword. Constructor reinitialize object only with developer or user given values.

Responsibilities of new keyword and constructor in creating object:
the responsibilities of new keyword is
- in identification phase it creates non-static variables with default values in heap area in continuous memory locations and also identifies non-static blocks.
- in execution phase it executes variables and non-static blocks in the order they defined, means it initialized non-static variables with assigned values and executes non-static block logic in JSA by creating separate stack frame.
- then after execution of non-static variable and non-static blocks, new keyword gives control back to constructor to perform initializes operations.

the responsibilities of Constructor is reinitializing non-static variables means object with developer or user given values as per its logic.
- then finally new keyword returns object reference to calling area, then it is stored in destination referenced variable.

No comments:

Post a Comment