Search code examples
programming-languages

Constructor and object intitiation


When we access "this \ Me" in the constructor of any class, how is it that "this" is already available while its yet getting constructed? Has a temporary creation of the instance already happened before the constructor call? If so then does this mean these Constructors are called after the actual object initialisation?


Solution

  • the object is created and the memory is allocated before you initialize it with the constructor.... ex 1. you create the object;

    MyObject myObject;
    

    2. you initialize it

    myObject = new MyObject();
    

    these 2 steps are also done when you are doing this:

    MyObject myObject = new MyObject();
    

    Edit: in the constructor this goes for myObject