This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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: