Return statement in a constructor

radha gogia

radha gogia

@radha-BTDzli Oct 17, 2024

why is non-parametrized return statement allowed in a constructor call when actually the constructor never returns anything,not even void.
what is the difference between the return statement written inside a method and the return statement inside a constructor,and if return inside a method says that it returns nothing means void ,then it should be so true for the constructor case also,but we say that constructor doesn't return anything not even void,so how will u explain this concept?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Sanyam Khurana

    Sanyam Khurana

    @sanyam-Nl7Zqc Nov 26, 2014

    radha gogiawhy is non-parametrized return statement allowed in a constructor call when actually the constructor never returns anything,not even void.
    what is the difference between the return statement written inside a method and the return statement inside a constructor,and if return inside a method says that it returns nothing means void ,then it should be so true for the constructor case also,but we say that constructor doesn't return anything not even void,so how will u explain this concept?

    Could you please elaborate your question may be by some code?

  • radha gogia

    radha gogia

    @radha-BTDzli Nov 26, 2014

    Sanyam KhuranaCould you please elaborate your question may be by some code?

    I understood that concept ,actually constructor implicitly return datatype is void ,so expilcitly we cant mention it ,just having one confusion that when the constructor doesn't return anything then actually how is that the reference of the newly created instance of the class is stored in the reference variable ,say when i write class obj=new class();

    so here when the constructor is called ,it will first initialize the variables of the object so how is it able to return the reference value for the newly created object and assign it to obj reference variable.

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Nov 26, 2014

    You need to look for Stack and Heap concepts and how it works.
    A return value is not assigned to reference but the memory reference of object created by right hand side.
    When you create an Object a memory reference will be created for newly created Object which is assigned to a variable.
    When you do String s = "ABCXYZ"; then, an String object is created and then memory reference of that object will be assigned to variable "s".

  • radha gogia

    radha gogia

    @radha-BTDzli Nov 26, 2014

    Anoop KumarYou need to look for Stack and Heap concepts and how it works.
    A return value is not assigned to reference but the memory reference of object created by right hand side.
    When you create an Object a memory reference will be created for newly created Object which is assigned to a variable.
    When you do String s = "ABCXYZ"; then, an String object is created and then memory reference of that object will be assigned to variable "s".

    Sir,you said memory reference is created,so is this reference is actually the address of that particular memory region within which object is created,so it means constructor calling merely initializes the instance variables and it is the combined effect of new operator and the classname() ,which actually creates the memory reference .am i rght or wrng??

  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Nov 27, 2014

    When you do the new Object(). JVM loads the class file in memory and creates an Object.
    Now if object is created it should be on some memory location. That memory location will be assigned to variable.

  • radha gogia

    radha gogia

    @radha-BTDzli Nov 27, 2014

    Anoop KumarWhen you do the new Object(). JVM loads the class file in memory and creates an Object.
    Now if object is created it should be on some memory location. That memory location will be assigned to variable.

    Actually ,I am clear with this thing ,the only confusion that prevails is that till we reach the end of constructor ,the memory reference is created ,so that reference value is created by the JVM or is it work of constructor itself and is there a stack formation which takes place when constructors are called like stack formation takes place in a function.

  • rahul69

    rahul69

    @rahul69-97fAOs Dec 7, 2014

    radha gogiawhy is non-parametrized return statement allowed in a constructor call when actually the constructor never returns anything,not even void.
    what is the difference between the return statement written inside a method and the return statement inside a constructor,and if return inside a method says that it returns nothing means void ,then it should be so true for the constructor case also,but we say that constructor doesn't return anything not even void,so how will u explain this concept?

    Constructor never returns anything, not even void, it means that u don't need to give it a return type (not even void), which is required for functions, ie even for fun() not returning anything, u will have to write void fun().

    radha gogiaSir,you said memory reference is created,so is this reference is actually the address of that particular memory region within which object is created,so it means constructor calling merely initializes the instance variables and it is the combined effect of new operator and the classname() ,which actually creates the memory reference .am i rght or wrng??

    Yes, constructor initializes the object. Also new provides the memory, and class name
    and object name creates the object (can be said so in a rough manner).