ObjectName

Hello,

Could somebody tell me how "ObjectName.getInstance(arg)" methods are different from "new ObjectName(arg)" , since I found internally each time you call "getInstance", it creates a new object itself. Is it added just to merely providea factory method. That's all..

Thanks in advance.

Deepak Kumar

[328 byte] By [DKumara] at [2007-11-26 16:12:34]
# 1

If you call new ObjectName(arg)

twice with the same value of arg, you will necessarily obtain two distinct but equal ObjectName instances, because you have explicitly constructed those instances. If you call ObjectName.getInstance(arg)

twice with the same value of arg, then you might get two instances or you might get the same instance twice. It happens that the current implementation in the JDK returns a new instance each time but a future version might change this.

The method ObjectName.getInstance(ObjectName) has the further advantage that it is guaranteed to return an instance of the ObjectName class itself and not a subclass, even if its argument is a subclass. This is useful when a subclass that overrides ObjectName methods might perturb the behaviour of your application.

emcmanusa at 2007-7-8 22:35:12 > top of Java-index,Core,Monitoring & Management...
# 2
Hi , Thank you for your quick reply. I am still not clear about functionality of getInstance(ObjectName). As it uses subObjName.getSerializedNameString(); & here again subclass can fool the super class.. Could you please give more insight on this.Thanks
DKumara at 2007-7-8 22:35:12 > top of Java-index,Core,Monitoring & Management...
# 3
The getSerializedNameString method is private so it can't be overridden by subclasses. It only accesses private fields and methods so its behaviour can't be changed by modifying or overriding those either.
emcmanusa at 2007-7-8 22:35:12 > top of Java-index,Core,Monitoring & Management...
# 4
Thank you very much. I didn't noticed that..Thanks for your time.
DKumara at 2007-7-8 22:35:12 > top of Java-index,Core,Monitoring & Management...