When does the Constructor gets called in java?

前端 未结 10 1363
[愿得一人]
[愿得一人] 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:15

    Constructor is what practically creates object. It is called when object is created by executing new MyClass() (or its parametrized version).

    0 讨论(0)
  • 2020-12-16 03:16

    The object memory is allocated, the field variables with initial values are initialized, and then the constructor is called, but its code is executed after the constructor code of the object super class.

    0 讨论(0)
  • 2020-12-16 03:17

    basically constructors are called to initialize the values of the instance variables except the case for default constructors. However, this initialization of the instance variables are done in 4 steps (as applicable):

    1. variables are initialized with default values (ints with 0, chars with u\0000 etc.)
    2. variables are initialized with explicit initialization values
    3. initialized with static blocks
    4. constructors are called
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题