We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class Singleton {
// Private non-parameterized constructor
private Singleton() {
instance = this;
}
// Public instance variable named `instance`
public static Singleton instance;
// Public static method named `getSingleInstance` that returns the single instance of the Singleton class
public static Singleton getSingleInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
// Print a line with the given input string
public void printHello(String message) {
System.out.println("Hello I am a singleton! Let me say " + message + " to you in java.");
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Singleton Pattern
You are viewing a single comment's thread. Return to all comments →
public class Singleton { // Private non-parameterized constructor private Singleton() { instance = this; }
}