It's not a secret anymore!
The moose likes Beginning Java and the fly likes Can we return null value from constructor of Object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Can we return null value from constructor of Object ?" Watch "Can we return null value from constructor of Object ?" New topic
Author

Can we return null value from constructor of Object ?

Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 202
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 !!



God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
Sona Patel
Ranch Hand

Joined: Mar 30, 2009
Posts: 75
You are right Ashish...

We can not return any value from constructor...

Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

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:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 202
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 !!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Can we return null value from constructor of Object ?
 
Similar Threads
Costructor return type?
destroying objects
Lookup key in HashTable
Generic clone/equals Methods
How to interpret results of println when printing user defined object