• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return type of a constructor!!  RSS feed

 
Sam Mites
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.......
This question have been bugging me for quite a long time..wh constructors have no return type?Coz..a constructor returns a object of its Class.......so will it be right to say that a constructor have a return type "Object"???


Thanks and regards

Sam
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because a constructor does not function in the way other methods do. Its purpose is to initialize the instance variables of the class when an instance is created.
 
Sam Mites
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry that i am not able to understand it fully...can u plzz elaborate a bit...
What i think is that it can have a return type of type object...
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor doesn't return anything. It just provides customized initialization for the object being constructed. If you are thinking that the constructor actually constructs the object and returns it, no, it doesn't. It doesn't return anything.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those areas where you don't want to over-think it. The simple answer is: "That's the syntax of the Java language." (There are more technical reasons, as alluded to by Keith and Paul).

An advantage of constructors not having a return type is that you can easily identify when something is a constructor or a method. For example, I know, when seeing it in documentation, that public Foo() is a constructor, and public void foo() and public String bar() are methods. One may try to argue that you can tell that by the capitalization of the name (i.e. Foo() is a constructor and foo() is a method), but remember capitalization rules are conventions and not syntax rules. In other words, the compiler (or Runtime environment) cannot and do not enforce them.

Lastly, if constructors did have "return" types, aside from the technical arguments for them not having them, the syntax would simply look funny: public Foo Foo().

I think the points Paul and Keith make are getting into the technicalities of the language, especially in terms of how object referencing works. If you want the nitty gritty details of such, I am sure they or someone else can provide further details if you desire, but as I said in my opening, my recommendation is to simply not over think it.

I hope that helps.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also notice that constructors don't get called like methods: you don't say

Foo foo = Foo();

but

Foo foo = new Foo();

The new operator is what actually creates (and "returns") the object, the constructor "just" initializes it. Or at least that's the way *I* interprete it...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!