Algorithm


Problem Name: Java Singleton Pattern

Problem Link: https://www.hackerrank.com/challenges/java-singleton/problem?isFullScreen=true

In this HackerRank Functions in Java programming problem 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. A private Singleton non parameterized constructor.
  2. A public String instance variable named str.
  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

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


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;
    }
}
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Java Factory Pattern in Java solution in Hackerrank - Hacerrank solution Java
Next
[Solved] Covariant Return Types in Java solution in Hackerrank - Hacerrank solution Java