Question: Consider the following code that use Singleton pattern to answer below questions? public class Singleton { private Singleton(){} public static Singleton getInstance(){ return SingletonHelper.INSTANCE; }

Consider the following code that use Singleton pattern to answer below questions?

public class Singleton {

private Singleton(){}

public static Singleton getInstance(){

return SingletonHelper.INSTANCE; }

private static class SingletonHelper{

private static final Singleton INSTANCE = new Singleton(); }

}

  1. (2 points) Does the above code ensure to have only one instance of class Singleton? Explain your answer?

  1. (1 point) Write a code to show how Class A can get an instance of Singleton class?

  1. (3 points) Is the above code considered thread-safe? Explain your answer?

  1. (4 points) Rewrite the above code to to make it thread-safe using the idea of synchronization?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!