*
The moose likes Ant, Maven and Other Build Tools and the fly likes java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Reply locked New topic
Author

java

venkataswamy gumma
Greenhorn

Joined: Dec 22, 2004
Posts: 1
i want details about constructor in java doesn't have return type?
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
This post doesn't belong to this forum. Java in General (beginner) seems to be more appropriate

Now, coming to your question: I think you want to know why the constructors doesn't return anything.
Let me try to explain with an example. Look at the following code


When the constructor is called? It is called when the new MyClass() is executed, right?
Assume for a moment that the constructor can return a value.
So, what will happen if the constructor did return something, say an int. It is similar to assigning a int value to the variable a, which is of type MyClass. This will definitely be an error.
So, the constructor, if at all can return something, can return only
a) an object of MyClass or
b) an object of some subclass of MyClass.
Returning anything else will trigger an error.

Now, you won't return an object of some subclass of MyClass, because the person calling the new MyClass() is expecting an object of MyClass and not it's subclass (otherwise he could have called new SubClassOfMyClass() straightaway)

This leaves us to one possibility: the constructor can return only the object of MyClass. So, why allow the constructor to return something, which might lead to potential problems? Better make it mandatory for the constructor not to return anything. Makes sense, isn't it?


Mani
Quaerendo Invenietis
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Wow, did the university courses start up already? Aren't you guys on Christmas break?

Since this question was already asked, and I mean, pretty much this question, just by someone else, I think it best to just close this thread and have the discussion continued where it started.


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java
 
Similar Threads
zipping as self extracting exe
J2EE
class
WA #2 ..... word association
c++ or Java--your suggestions please