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.