Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we return null value from constructor of Object ?

 
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
Pie
Posts: 20495
54
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 !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic