-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSingletonCloningExample.java
55 lines (45 loc) · 1.64 KB
/
SingletonCloningExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.javaexperiments;
/**
* Using Cloneable Interface, we can create more than one instance in a Singleton class by
* simply using the clone() on the first object
*/
public class SingletonCloningExample implements Cloneable {
private static SingletonCloningExample single_instance = null;
@Override
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
/**
* Making the constructor as private
*/
private SingletonCloningExample() {}
/**
* Static method to create instance of Singleton class
* @return single object of 'SingletonCloningExample' class
*/
public static SingletonCloningExample getInstance() {
/**
* Ensuring only one instance is created
*/
if (single_instance == null)
single_instance = new SingletonCloningExample();
return single_instance;
}
public static void main(String[] args) throws CloneNotSupportedException {
/**
* Instantiating SingletonCloningExample class with variable 'objectOne'
*/
SingletonCloningExample objectOne = SingletonCloningExample.getInstance();
/**
* Cloning the 'objectOne' using clone()
*/
SingletonCloningExample objectTwo = (SingletonCloningExample) objectOne.clone();
/**
* Checking the hashCode for both the objects which would be different,
* meaning the objects are different
*/
System.out.println("Hashcode of Object 1 - " + objectOne.hashCode());
System.out.println("Hashcode of Object 2 - " + objectTwo.hashCode());
}
}