Monday, April 11, 2005

Why you cannot enforce singleton-ness for classes which implement eclipse extensions and learning it the hard way:)

Yes, the title should have given you a good idea about what this is about...

So it happened that in my enthusiasm I marked as "private" the contructor of a class implementing some extension..after all a singleton class should only have a public getInstance() method which provides the only instance and the clients should have no other way to instantiate the class.

However, if you do this for a class implementing an extension , there is a slight problem..the plugin framework will try to instantiate this class using reflection and fail. If you are the unfortunate extension writer :) you will dig deep and yet have to dig deeper to find that an IllegalAcessException is thrown during the "Init" of the instance from somewhere near the classloader and only then shall epiphany dawn that the constructor you made private could be the reason.

So singleton or not..keep the contructors public :)

No comments: