Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we return null value from constructor of Object ?  RSS feed

 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Members,

Just tell me !

Can we return null value(or any value) from constructor of Object ? As far as I know we can't .So please tell me Am I right ?


Thanks In Advance !!


 
Sona Patel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Ashish...

We can not return any value from constructor...

Regards
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor does not return anything; the "new" operator returns an object that has been initialized using a constructor.

If you really want a constructor that may return null, perhaps you should check out the factory method pattern. A short example:
 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:A constructor does not return anything; the "new" operator returns an object that has been initialized using a constructor.

If you really want a constructor that may return null, perhaps you should check out the factory method pattern. A short example:


Thanks Rob !!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!