Hi Can someone please explain me Singleton pattern in JAVA?
When i googled it, I found following information:
Singleton pattern cause only one Object of a class will be used through out, in Order to get that single Object of class we must implement 1) getInstance() method in the class.. and that must be declare as static
2) constructor should be private for that class, in order to avoid new SingletonClass() outside the singleton class...
3) there should be one private static variable with reference type equal to singleton class..
Is there anything I missed out, pleases remind me...?
One important thing, if the constructor does not throw an exception then initialize the instance in a static block or inline with variable declaration. However, there are huge number of people who discourage the use of singleton. This javaranch FAQ talks about singleton and has a lot of links.