File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Constructors in Java does not have return type?

 
pradeep chellappan
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64181
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Pie
Posts: 9472
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64181
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9472
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9472
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:(Sorry. Kind of. )

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

And BTW, I totally agree.

Winston
 
Jomar Belen
Greenhorn
Posts: 1
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jomar Belen wrote:
yeah so why no return type?


That's already been answered. Please do read the responses before posting.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic