HackerRank Java- Singleton Pattern

Complete the Singleton class in your editor which contains the following components:

A private Singleton non parameterized constructor.
A public String instance variable named str.
Write a static method named getSingleInstance that returns the single instance of the Singleton class.
Once submitted, our hidden Solution class will check your code by taking a String as input and then using your Singleton class to print a line.

  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. import java.lang.reflect.*;
  7. class Singleton {
  8. private Singleton() {}
  9. public String str;
  10. private static Singleton instance = null;
  11. public static Singleton getSingleInstance() {
  12. if (instance == null) {
  13. instance = new Singleton();
  14. }
  15. return instance;
  16. }
  17. }

download android app