• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Constructors in Java does not have return type?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 930
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is good explanation

http://stackoverflow.com/questions/1788312/why-a-constructor-does-not-return-a-value
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read this line on Java 2 the Complete Reference by Herbert Schildt (5th Edition)

Constructors look a little strange because they have no return
type, not even void. This is because the implicit return type of a class’ constructor is the
class type itself.

page no. 145

What does it mean then ?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What it boils down to for me is that you really shouldn't try to put too much meaning into it. I don't necessarily agree with the characterization that a constructor has an "implicit" return type. You use a constructor with the new keyword. That's just how Java was designed. It's the combination of new SomeConstructor… that gives back a reference to the new object that was created and initialized. I don't know why there is any need to look for some kind of deeper "meaning" to all that. Maybe that's just me though, I don't know. :shrug:
 
this is supposed to be a surprise, but it smells like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic