Return statement in a constructor

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

  • Sanyam Khurana
    Sanyam Khurana
    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?
  • radha gogia
    radha gogia
    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.
  • Anoop Kumar
    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".
  • radha gogia
    radha gogia
    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??
  • Anoop Kumar
    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.
  • radha gogia
    radha gogia
    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.
  • rahul69
    rahul69
    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).

You are reading an archived discussion.

Related Posts

This discussion will be opened at 8:00 PM (IST) sharp. All participants will be able to post here entries here.
The Indian mobile manufacturing company, Karbonn has quietly launched three new smartphones: Karbonn Titanium S4Plus, Titanium S8 and Titanium S6. These three new smartphones are now available with online retailers...
With an objective to manufacture spare parts and tools in space to save transport costs as well as time, NASA’s 3D printer on the International Space Station (ISS) has successfully...
Quote: Have you ever heard of Buckypaper? It is one of the strongest materials known to man. This carbon nanotechnology material is 500 times stronger than steel and ten times...
Since many of you requested for the questions asked in the Rapid Fire Quiz. Here we go - (Question): This formula represents (A): Newton's First Law of Motion (B): Newton's...