Skip to content

Instantly share code, notes, and snippets.

@codinko
Created October 13, 2021 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/fbd3dc9437d1ee7e0496c4d6fa92c90d to your computer and use it in GitHub Desktop.
Save codinko/fbd3dc9437d1ee7e0496c4d6fa92c90d to your computer and use it in GitHub Desktop.
class Singleton {
private static Singleton instance = null;
public String s;
private Singleton()
{
s = "hey i am ston";
}
public static Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
}
class GFG {
public static void main(String args[])
{
Singleton x = Singleton.getInstance();
Singleton y = Singleton.getInstance();
Singleton z = Singleton.getInstance();
System.out.println("Hashcode of x is " + x.hashCode());
System.out.println("Hashcode of y is " + y.hashCode());
System.out.println("Hashcode of z is " + z.hashCode());
// Condition check
if (x == y && y == z) {
System.out.println( "Three objects point to the same memory location on the heap i.e, to the same object");
}
else {
System.out.println("Three objects DO NOT point to the same memory location on the heap");
}
}
}
OUTPUT:
----------
Hashcode of x is 876908723
Hashcode of y is 876908723
Hashcode of z is 876908723
Three objects point to the same memory location on the heap i.e, to the same object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment