Java Singleton Pattern Hacker Rank Solution

Hello coders, In this post, you will learn how to solve Java Singleton Pattern Hacker Rank Solution. This problem is a part of the Java programming series. 

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

Java Singleton Pattern Hacker Rank Solution
Java Singleton Pattern Hacker Rank Solution

Java Singleton Pattern Hacker Rank Solution


“The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.”
– Wikipedia: Singleton Pattern

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

  1. private Singleton non parameterized constructor.
  2. public String instance variable named .
  3. 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.

Input Format

You will not be handling any input in this challenge.

Output Format

You will not be producing any output in this challenge.

Sample Input

hello world

Sample Output

Hello I am a singleton! Let me say hello world to you

Java Singleton Pattern Hacker Rank Solution

import java.util.Scanner;
import java.lang.reflect.Constructor;
class Singleton {
    private Singleton() {}
    public String str;
    private static Singleton instance = null;
    public static Singleton getSingleInstance() {
        if (instance == null) {
            instance = new Singleton();
        return instance;

Disclaimer: The above Problem (Java Singleton Pattern) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Sharing Is Caring

Leave a Comment