We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Because when you call getSingleInstance(), it will return a new object every time. In other words, you can make an unlimited number of objects with that method.
By making a private object (in this case, the object "instance"), your function doesn't return a new object each time - it returns the only Singleton object available, which is "instance" and you can't make any more objects from the Singleton class (which is exactly the idea behind the Singleton pattern).
Java Singleton Pattern
You are viewing a single comment's thread. Return to all comments →
Because when you call getSingleInstance(), it will return a new object every time. In other words, you can make an unlimited number of objects with that method.
By making a private object (in this case, the object "instance"), your function doesn't return a new object each time - it returns the only Singleton object available, which is "instance" and you can't make any more objects from the Singleton class (which is exactly the idea behind the Singleton pattern).
I hope this cleared up any confusion.