CrazyEngineers
  • Return statement in a constructor

    radha gogia

    Member

    Updated: Oct 17, 2024
    Views: 2.0K
    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?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Sanyam Khurana

    MemberNov 26, 2014

    radha gogia
    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?
    Could you please elaborate your question may be by some code?
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberNov 26, 2014

    Sanyam Khurana
    Could 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberNov 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".
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberNov 26, 2014

    Anoop Kumar
    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".
    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??
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberNov 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.
    Are you sure? This action cannot be undone.
    Cancel
  • radha gogia

    MemberNov 27, 2014

    Anoop Kumar
    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.
    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.
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberDec 7, 2014

    radha gogia
    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?
    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 gogia
    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??
    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).
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register