Created
October 13, 2021 17:28
-
-
Save codinko/fbd3dc9437d1ee7e0496c4d6fa92c90d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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