aspose file tools*
The moose likes Beginning Java and the fly likes Why Constructors in Java does not have return type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Constructors in Java does not have return type?" Watch "Why Constructors in Java does not have return type?" New topic
Author

Why Constructors in Java does not have return type?

pradeep chellappan
Greenhorn

Joined: Oct 20, 2005
Posts: 29
Experts,

I have gone through many explanations on internet to understand why constuctors in java does not have return type. But i couldn't get a clear/satisfictory/convincing answer. Can any one please let me know the "exact" reason.

Regards,
Pradeep
sai rama krishna
Ranch Hand

Joined: May 29, 2009
Posts: 269
Here is good explanation

http://stackoverflow.com/questions/1788312/why-a-constructor-does-not-return-a-value
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

They don't have a return type because they don't return anything. They don't even need to be declared void because there is no possibility of them returning anything. Methods need a declared return type because they can return any type at all, or nothing. But with constructors, since the programmer, the compiler, and the JVM all know that they don't return anything, there's no point in explicitly stating that.

Additionally, it makes it easy for the language definition to distinguish between a c'tor and a method.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61430
    
  67

sai rama krishna wrote:Here is good explanation

I don't think it's all that good. Jeff's explanation is better.

In fact, think about it practically for a minute. What would you actually do with a return value? Of what possible use could it be?

You can't call constructors directly -- you invoke them via the new operator, which returns the newly-construced object instance. So where and how would you use a return value that you cannot even obtain?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Bear Bibeault wrote:You can't call constructors directly -- you invoke them via the new operator

Hmmm. So what are super() and this() then?

@Pradeep: This minor nitpick aside, Bear's quite right in general: You can't write:
String s = String();
which is how you would normally call a method; you must put new in front of it. So what you're in fact saying is:
"new, do your stuff to create me a new instance of {whatever-class} using the constructor I provided"

Direct invocations of constructors are only allowed in the first line of a constructor method and, as you probably already know, if you don't put one in yourself, the compiler will add super() for you.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61430
    
  67

Winston Gutkowski wrote:
Bear Bibeault wrote:You can't call constructors directly -- you invoke them via the new operator

Hmmm. So what are super() and this() then?

Devil's advocate, eh?

Those are not method calls. They are a notation that can only appear at certain points in the code. The fact that they look similar to method calls doesn't make them so.

For example, you can't do: xyz = this(); and you can't use them just anywhere in the code.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:So what you're in fact saying is:
"new, do your stuff to create me a new instance of {whatever-class} using the constructor I provided"


Just to nitpick a bit more, I would phrase it as, "new, do your stuff to create me a new instance of {whatever-class} and then initialize that newly created instance using the constructor I provided." The only difference being that I think this makes it clear that new creates the object, not the constructor.

Direct invocations of constructors are only allowed in the first line of a constructor method


Er, first statement, actually.



(Sorry. Kind of. )
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Bear Bibeault wrote:Devil's advocate, eh?

Yeah, sorry. Friday.

Those are not method calls...

Well maybe not, but they are used to transfer control to a specific constructor in much the same way. And since we do agree that ctor's don't return anything, xyz = this() isn't really relevant, since the same would be true of a void method.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Jeff Verdegan wrote:(Sorry. Kind of. )

Nah, that's all right. I started it.

And BTW, I totally agree.

Winston
Jomar Belen
Greenhorn

Joined: Oct 13, 2012
Posts: 1

pradeep chellappan wrote:
I have gone through many explanations on internet to understand why constuctors in java does not have return type. But i couldn't get a clear/satisfictory/convincing answer. Can any one please let me know the "exact" reason.


yea i have been wodering about that too.

well from what i understand, constructors in java is kinda like the first thing that will be executed the the class is instantiated.



that will automatically call the constructor of the class MyClass. kinda like an initialization part of the class.
though it can have parameters and can be overloaded like a method...



yeah so why no return type? maybe it's not/not treated as a method? :{
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jomar Belen wrote:
yeah so why no return type?


That's already been answered. Please do read the responses before posting.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Jomar Belen wrote:yeah so why no return type? maybe it's not/not treated as a method? :{

I think others have explained why they don't have a return type, but just to be clear: a constructor is not a method. Here are some relevant quotes from the Java Language Specification.

The body of a class declares members (fields and methods and nested classes and interfaces), instance and static initializers, and constructors

Constructors (ยง8.8) are similar to methods, but cannot be invoked directly by a method call; they are used to initialize new class instances.

Constructor declarations are not members.

Constructors are never invoked by method invocation expressions
 
wood burning stoves
 
subject: Why Constructors in Java does not have return type?