• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In java, does Constructor return anything?

 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an argument with my friend. He told me that a constructor returns address of the object. And I disagree with him. So what do you think guys.. Special thanks for the answer which has 'reference'.
 
Stephan van Hulst
Bartender
Posts: 6318
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors don't return anything. They simply setup an object for use. The new keyword returns a reference to a constructed object.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor doesn't return anything. The call to new returns the reference to the newly created object(address or pointer to the object is not a Java term. In Java it's called reference. However, the idea is the same). Internally, the JVM calls the constructor of the object after it creates the object.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Robby
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. I was also giving the same argument(by wild guess). It would be more helpful if you give me the reference of the source(any book or website from where you got that info)
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Language Specification tells you all you ever wanted to know about the specification of the Java language. Here's a link to a page where you can download it from: Java Language Specification.

Be warned that it isn't an easy document to read, especially for beginners.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This section covers it, although it doesn't explicitly say that Constructor doesn't return an address.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty trivial to demonstrate that a constructor cannot have a return statement in it, and so in this sense a constructor does not return anything. Is that what your friend is saying, though? He may mean something a little different.

JLS 15.9.4 may be relevant:
The value of a class instance creation expression is a reference to the newly created object of the specified class.


Looked at from the outside, a constructor looks much like a special method that returns a reference to a newly created instance. Except for the additional keyword "new" in the calling syntax. That's not how they are defined in the JLS though.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thankful to you guys for your kind reply: Stephan van Hulst, Jayesh A Lalwani, Seetharaman Venkatasamy, Paul Clapham, Mike Simmons.

Mike, my friend is saying "constructor does return the address of newly created object to the class instance". And I told him this is the job of 'new' keyword. But we both don't have any written proof to support our statement. Though here, you guys are favoring my statement. (I know "The value of a class instance creation expression is a reference to the newly created object of the specified class")
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not to forget that you can *return* nothing inside constructor. i.e,
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!! Mr. Seetharaman.. It's a very good point. And it's also showing the error if I put 'return null;': "cannot return a value from method whose result type is void" Thanks Again!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor is a special block of code that is called by the JVM to initialize a newly created object. It's the job of the new operator to allocate memory, call the constructor to initialize the object in the allocated memory and then to return a reference to the object.

Note that a constructor is not a method, even though it superficially looks like a method.

You cannot call constructors directly. It doesn't really make sense to say that a constructor returns a reference to an object. Constructors simply don't return anything.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's some more concrete evidence.

I've just compiled the following code:

Then I looked at the byte code generated:


According to the list here: Java bytecode instruction listings, return is the command to "return void from method". That's what the constructor uses. There are other return commands used to return Objects or primitives (areturn, ireturn, lreturn, dreturn, freturn).

So in terms of what the constructor actually gets compiled into, it doesn't return anything.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Jesper and Special Thanks! to you Matthew. Matthew, your info is more insightful. Now I close this post. I'm Thankful to all of you guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic