-1

Looking through some Java code the other day, I saw something I haven't seen before and am slightly confused on. Part of the code is shown below:

public class PreferredAlternateProxy {
    ...
    private static PreferredAlternateProxy instance;
    ...

    public static PreferredAlternateProxy getInstance(){
        if(instance == null)
            createInstance();

        return instance;
    }

    private static synchronized void createInstance(){
        if(instance == null)
            instance = new PreferredAlternateProxy();
    }
}

What is the purpose of declaring a static property of of the same class as the public class? In what situation is this useful?

What is the purpose of making the second method (createInstance()) synchronized?

Tony Scialo
  • 5,457
  • 11
  • 35
  • 52
  • `"What is the purpose of making the second method (createInstance()) synchronized?"` -- so can lazily create one and only one instance. – Hovercraft Full Of Eels Aug 02 '16 at 22:06
  • This code, was it completely out of context? Or did it have documentation, comments, or other explanations surrounding it? – Sotirios Delimanolis Aug 02 '16 at 22:10
  • It was sent to me as a guide to follow when creating a similar class, so no context or comments really – Tony Scialo Aug 02 '16 at 22:12
  • 2
    "What is the purpose of making the second method (createInstance()) synchronized?" It's called [double-checked locking](https://en.wikipedia.org/wiki/Double_checked_locking). – Andy Turner Aug 02 '16 at 22:12
  • What was the purpose of the similar class? – Sotirios Delimanolis Aug 02 '16 at 22:13
  • The purpose was to call a soap webservice – Tony Scialo Aug 02 '16 at 22:13
  • @AndyTurner: thanks for the info and the link. I had not heard of this term before. – Hovercraft Full Of Eels Aug 02 '16 at 22:15
  • thanks for the tip about singleton's, I was unaware of this design pattern. For those interested, i used this link http://www.javaworld.com/article/2073352/core-java/simply-singleton.html and this link http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern for a quick intro to the topic – Tony Scialo Aug 02 '16 at 22:31
  • This is code is incorrect. Because callers of `getInstance()` aren't required to synchronize with the thread that calls `createInstance()`, they may see the instance in an inconsistent or damaged state. – erickson Aug 02 '16 at 22:39
  • 1
    @Tony you might also want to read [what is so bad about singletons?](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons). – Andy Turner Aug 02 '16 at 22:39

2 Answers2

1

There is something like Design Patterns that tell you the ways to efficiently write a code. The example you gave is called a Singleton. The purpose of this pattern is to create one and the only one instance of the class. It requires a:

  1. Private constructor(s) - that unables anyone from creating the instance of the class.
  2. Private static field - that will keep the generated instance in the memmory.
  3. Public static method - that is the "gatekeeper", whos job is to return the instance.

Example:

public class Singleton() {
    private static Singleton instance;

    private Singleton() {

    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

The above example is called Lazy Initialization, because the instance is not created until you call getInstance() for the first time.

Below example shows Early Initialization, which creates the instance right when the class loads into the memmory.

public class Singleton() {
    private static Singleton instance = new Singleton();

    private Singleton() {

    }

    public static Singleton getInstance() {
        return instance;
    }
}
Humberd
  • 2,794
  • 3
  • 20
  • 37
0

The code that you have in your question is implementing Java Singleton Pattern.

  • PreferredAlternateProxy in your example is static to create a class level variable as you need only one instance.
  • Second Method is added to make it thread safe and also reduce the cost of directly invoking a synchronized method. Although we need it only for the first few threads who might create the separate instances. It is called double checked locking principle. So if instance is created then you are no more invoking a synchronized method.

Hoping this helps you understand it better.

Cheers !

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42