Is this the way Constructor in Java allocates memory?

前端 未结 5 1763
遥遥无期
遥遥无期 2021-01-07 04:45

Default constructor is automatically called after an object is created.

But in Java when we allocate memory using new operator i.e. classname obj = new classna

5条回答
  •  一向
    一向 (楼主)
    2021-01-07 05:16

    Default constructor is automatically called after an object is created.

    Yes (if the default (no-argument) constructor was called).

    Can someone explain me the same using a clear example.

    I'm not sure I understand your question to 100 % but no where in the Java Language Specification it says that the constructor should run it's code before the memory for the object has been allocated.

    This is explained in great detail with an example in Section 12.5: Creation of new Class Instances in the Java Language Specification.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题