-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.java
More file actions
58 lines (48 loc) · 1.06 KB
/
Singleton.java
File metadata and controls
58 lines (48 loc) · 1.06 KB
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
56
57
package NestedClass;
class CoffeeMachine
{
private float coffeeQty;
private float milkQty;
private float waterQty;
private float sugarQty;
static private CoffeeMachine my = null;
private CoffeeMachine()
{
coffeeQty=1;
milkQty=1;
waterQty=1;
sugarQty=1;
}
public void fillWater(float qty)
{
waterQty=qty;
}
public void fillSugar(float qty)
{
sugarQty=qty;
}
public float getCoffee()
{
float amount;
amount = waterQty + milkQty;
return amount;
}
static CoffeeMachine getInstance()
{
if(my==null)
my=new CoffeeMachine();
return my;
}
}
public class Singleton
{
public static void main(String[] args)
{
CoffeeMachine m1=CoffeeMachine.getInstance();
CoffeeMachine m2=CoffeeMachine.getInstance();
CoffeeMachine m3=CoffeeMachine.getInstance();
System.out.println(m1+" "+m2+" "+m3);
if(m1==m2 && m1==m3)
System.out.println("Same");
}
}