aspose file tools*
The moose likes Java in General and the fly likes In java, does Constructor return anything? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "In java, does Constructor return anything?" Watch "In java, does Constructor return anything?" New topic
Author

In java, does Constructor return anything?

Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
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

Joined: Sep 20, 2010
Posts: 3647
    
  17

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
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

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

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch Robby
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
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
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

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
Bartender

Joined: Jan 17, 2008
Posts: 2402
    
  28

This section covers it, although it doesn't explicitly say that Constructor doesn't return an address.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Dec 11, 2012
Posts: 45
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

Joined: Jan 28, 2008
Posts: 5575

not to forget that you can *return* nothing inside constructor. i.e,
Robby Ames
Ranch Hand

Joined: Dec 11, 2012
Posts: 45
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

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

Joined: Dec 11, 2012
Posts: 45
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In java, does Constructor return anything?