It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

Prashant Ameta
Greenhorn

Joined: Jul 17, 2012
Posts: 11
I know that Constructors dont have a return type.... i would like to know WHY??
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

They do.

It's an instance of the Class being "constructed", just not specified on the constructor signature.

What else are you "constructing" ?

WP
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

If they did return a type (other than the class being constructed), how would you save both? currently you do this:

now, assume the FooBar constructor returned an int. How do you save the returned int AND the reference to the new object?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Prashant Ameta
Greenhorn

Joined: Jul 17, 2012
Posts: 11
Thanks Sir,
I am quite new to JAVA,i am following Head first Java...and it says Constructors dont have a return type , not even void...and same is the difference between a method and a Constructor....

Now , after a month or two,if an interviewer asks me "Does a COnstructor have a return type or not??" I shall say Yes,Right???
Prashant Ameta
Greenhorn

Joined: Jul 17, 2012
Posts: 11
Thanks a lot
Its crystal clear Now..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

Prashant Ameta wrote:Now , after a month or two,if an interviewer asks me "Does a COnstructor have a return type or not??" I shall say Yes,Right???

I would say "It doesn't have a DECLARED return type, but it does return a reference to the newly created object".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Prashant Ameta wrote:Its crystal clear Now..:)


I'm glad you think so.

Because it certainly isn't to me, after reading those answers. The way I see it is this: when you declare a constructor in Java, you don't declare a return type, otherwise it would be a method. That much is clear. So does that mean that a constructor doesn't "have" a return type?

It also isn't true that a constructor returns an object of the class in which it's declared. Consider this code:



Now as you should already have learned if you're going to be asked questions like that, the FooBar() constructor implicitly has a line of code



inserted as its first line by the compiler. That line of code causes its superclass's constructor to be called. And since the superclass is Object, that constructor is



Now, notice that although this Object constructor is called, it doesn't return an Object. All it does is to initialize the Object class's state in the FooBar object which is being initialized. And likewise the FooBar constructor doesn't return a FooBar, all it does is to initialize the FooBar class's state in the FooBar object. So in fact a constructor doesn't return anything, all it does is to initialize the state of an object.

So what is it which does return a FooBar object? It's the "new" operator which does that.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

William P O'Sullivan wrote:They do.


No, they don't. The new operator creates the object and returns a reference to it. The c'tor merely initializes the existing, newly created object. It doesn't return anything.
Raja Pathak
Greenhorn

Joined: Sep 21, 2012
Posts: 4
How is the reference returned when contructor is created and what happens when we dont create constructor for any program??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raja Pathak wrote:How is the reference returned when contructor is created


As already stated, twice, the new operator returns a reference to the newly created object.

and what happens when we dont create constructor for any program??


If you don't define any constructor, the compiler defines one for you that takes no arguments and does nothing except call super().
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The constructor does not really return the reference. A constructor is just a special block of code that's called to initialize a new object. As Jeff says, the object itself isn't created by the constructor - it's actually the new operator that creates it, and it calls the constructor to initialize the new object.

If you don't provide any constructors in your class, then the Java compiler will automatically add one for you that looks like this:

Reference: Java Language Specification section 8.8.9


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor
 
Similar Threads
How to do timepass?
Jakob Nielsen Declares the Letter "C" Unusable
How To Design Better Software Using FDD - November 26-27, 2008, Hamburg, Germany
Naming convention?
Object.toString()