When does the Constructor gets called in java?

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

    At the byte code level.

    1. An object is created but not initialised.
    2. The constructor is called, passing the object as this
    3. The object is fully constructed/created when the constructor returns.

    Note: The constructor at the byte code level includes the initial values for variables and the code in the Java constructor. e.g.

    int a = -1;
    int b;
    
    Constructor() {
       super();
       b = 2;
    }
    

    is the same as

    int a;
    int b;
    
    Constructor() {
       super();
       a = -1;
       b = 2;
    }
    

    Also note: the super() is always called before any part of the class is initialised.


    On some JVMs you can create an object without initialising it with Unsafe.allocateInstance(). If you create the object this way, you can't call a constructor (without using JNI) but you can use reflections to initialise each field.

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

    THE JVM will first allocate the memory for your object, then initialize all fields, then invoke your constructor.

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

    After the Object creation

    once an object is created using new operator like Student s = new Student(); first Student object is created, and then constructor is called to initialize the variable of the object

    We can prove constructor is called after creating the object by using below code

    here we are using instance block. And also instance block is executed before the constructor

    so I am printing hash code in three places

    1. Inside Instance block
    2. Inside Constructor
    3. Inside main mehtod

    all these three times hash code is equal, that means object is created before the constructor is executed

    because having a hash code means, there must be an object. And if hash code printed inside both instance block and constructor is equal. that means object must be created before the constructor execution

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

    The constructor gets called when a new object is created.

    NewObject n = new NewObject();
    
    public class NewObject {
        public NewObject() {
            // do stuff when object created
        }
    }
    

    Hope this helps.

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

    Given those options, 1. Before object creation.

    After the constructor finishes, the object has been created.

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

    It gets called at object creation. The memory must be reserved first for the object, otherwise the constructor code could not run. So maybe we could say after object creation. Also note that initialization code written in the class gets called before the constructor code.

    public Ex {
    
        int xVal = -1;
        int yVal;
    
        public Ex() {
            // xVal is already -1.
            //yVal defaults to 0.
        }
    }
    
    0 讨论(0)
提交回复
热议问题