• 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

Costructor return type?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

Why there is no return type of constructors'?
When we do something like:



Constructor of class do return something rite?
Whether it may be reference or allocated memory address.Then why there is no return type for it?

Regards,
Rock
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rock Xen:
Constructor of class do return something rite?



Wrong.

A constructor is not a method. It does not have any return type. Its primary function is to initialise the non-default fields of the object.

When you do "new MyClass()", it is the "new" operator that returns an object. You are not directly calling the constructor. Java does that indirectly, as a result of using the "new" operator.
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rock Xen:
........
Constructor of class do return something rite?
....



Normal scenario is that,we use constructor to initialize values.
I haven't seen a situation where constructor returns some information.
And i suppose you cannot achieve that one.
And one more thing is that,the absence of return type only makes a difference between normal methods and constructors.

Here what matters is the new, which has return type.The new object is, in effect, part of the input to the constructor.
 
Rock Xen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys!

I am agreed with what you have posted. I was just wondering... if that's the case i.e. "new" has got the return type and also JVM does that automatically, then lets say if I wanted to break the initialization of variables inside any constructor depending upon certain condition then how could I do that. I mean, if I would have any return type of constructor I would have said "return null" or "return something".
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If for some reason you can't initialize the object properly in the constructor, then you can throw an exception from the constructor.

There is no way to make a constructor return a value.
 
reply
    Bookmark Topic Watch Topic
  • New Topic