When does the Constructor gets called in java?

前端 未结 10 1410
[愿得一人]
[愿得一人] 2020-12-16 02:14

When does the Constructor get called?

  1. Before object creation.
  2. During object creation.
  3. After object creation.
10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 03:20

    Whenever we create an object by using 'new' operator then 1st task is performed by the new i.e. it allocates the memory for object in heap with pointing to the reference variable in stack and set the initial values of object fields.then it calls the constructor with passing 'this' as object to initialize it according to your requirement...

    So the constructor is always called after the object creation....

    Note: When you enter in constructor so 'this' keyword is working means your object has been created.

提交回复
热议问题