1

The core question is in the title but the answer I believe is right is as follows;

  • It ensures that only one instance is created

  • It provides a global point of access to an object

I am asking because I am unsure that the above is correct answer to the question. I did a little research but the above answer keep coming up. Is there anything to add or is the answer wrong completely.

Aleksandar Zoric
  • 1,343
  • 3
  • 18
  • 45
  • 2
    See [Singletons](http://stackoverflow.com/documentation/java/130/singletons#t=201612141638581578711) – JonK Dec 14 '16 at 16:40
  • What is the relevance of the “*on the JVM*” phrase or the `jvm` tag of your question? Besides, you already named the “essential elements of the Singleton pattern”, at least as specified in the famous book “Design Patterns” of the GoF, though there are people questioning whether the second bullet, which basically describes a (possibly encapsulated) global variable, is really an essential part of the Singleton pattern. Since design patterns are not carved into stone, there is no absolute right or wrong here. – Holger Dec 15 '16 at 18:50
  • Well that JVM part of the question is where I was confused at for the very same reason. The question was asked by one of my lectures in college and it was written as above. Now personally, I would have just asked what are the essential elements but the fact that the JVM was mention, I thought there may be another answer to this simple question. – Aleksandar Zoric Dec 16 '16 at 15:15
  • Does this answer your question? [Java Singleton Pattern](https://stackoverflow.com/questions/2832297/java-singleton-pattern) – pringi Mar 04 '22 at 21:23

2 Answers2

2

Yes you are correct. Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, drivers objects, caching and thread pool.

0

Singleton pattern is not difficult to understand.

Singleton it use to limit the numbers of object instance to one. Generally it use with factories.

Singleton class is like this :

public class YourClass{
    private static final YourClass INSTANCE = new YourClass();
    private YourClass{Your code..}
    //This is the factorie
    public static YourClass getInstance(){return INSTANCE;}
} 

You can't create many instance of your class. The program return always the same object.

You can find many other explanation on this page : https://www.tutorialspoint.com/java/java_using_singleton.htm

Mattasse
  • 1,023
  • 1
  • 11
  • 18
  • Please take a look at: [Are answers that just contain links elsewhere really “good answers”?](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) – Pshemo Dec 14 '16 at 16:44
  • 1
    Will read through it now. Thanks a lot matey. I understand how to implemented the pattern in code and what it does etc. But I was not sure if there is more to than the two bullet point in the question. Either way, will look into the link you suggested. Thank you once again. – Aleksandar Zoric Dec 14 '16 at 17:52