• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does Constructor returns something?  RSS feed

 
Vimlesh Kumar Patel
Greenhorn
Posts: 20
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What constructor return?

Below is code:



In above statement what actually happening.

I'm not so sure about this but I've some point please make me sure about them.

1. Either new keyword is creating an object of Dog and assigning to a.
2. or new Dog() is returning the object of the type of which it belongs with the default values for that object or with the values specified in the argument (ex. 10 here).
3. even something else i don't know..
Please give me correct idea what actually happening here.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor returns the newly created object
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ole Kristian Sandum wrote:The constructor returns the newly created object

Not exactly.

The new operator returns the newly-created instance; constructors themselves are never called directly and return nothing.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor implicitly return reference id of the object which creates using 'new' operator that's why it doesnot return anything else.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Constructor implicitly return reference id of the object which creates using 'new' operator that's why it doesnot return anything else.

Tushar, I suggest you read Bear's post, because what you said is incorrect. Constructors do not return anything. Indeed, they aren't even methods in the strict sense, even though they look like them. It is new that returns the reference.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t would probably be better to say that the constructor uses the reference returned by the new operator. Let's see whether there is anything useful in the Java Language Specification. No, there wasn't. Nor in the JVM Specification.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the disassembled .class file - which you normally probably wouldn't - you'll see the following for a simple class called Example with only a default constructor and a main method that uses it:


At line 32 the byte code instruction new #1 creates a new object of the type com.coderanch.Example. At line 34 the byte code instruction invokespecial #16 is the invocation of Example's default constructor, which you can piece together by looking up #16 in the constant pool and walking thru the references to the other constants. The comment at the end of the line represents the result of doing that. Given these two instructions it is clear that the constructor is not responsible for creating the object, it is "merely a method" called on it which has a special name: <init>. As for what the constructor returns, if you look at line 28 you'll see a return instruction with no associated reference, which represents a void return. Also, if you backtrack to the line above and look at the last characters of the comment line, you'll see ()V, which indicates a void return type.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you get all that with javap -c Example? I have used javap many times and never seen that constant pool before.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, with the -verbose option.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Verbose? Of course! Thank you
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks all.. Till date i thought new operator creates an object and constructor use that reference and then implicitly returns it(several books mentioned that)

Thanks for correcting it with proof..
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't look like anybody has said this, so let me just add that the purpose of the constructor is to initialize the state of the instance.
 
Vimlesh Kumar Patel
Greenhorn
Posts: 20
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much "JavaRanch Team"

finally I'm clear what constructor is actually doing?

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!