-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSingleTonDemo.java
More file actions
35 lines (31 loc) · 995 Bytes
/
SingleTonDemo.java
File metadata and controls
35 lines (31 loc) · 995 Bytes
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
public class SingleTonDemo {
public static void main(String[] args) {
Car car1 = Car.createObj("BMW", 4, 6);
System.out.println(car1.name);
System.out.println(car1.wheelCount);
System.out.println(car1.seatCount);
System.out.println("******************************");
Car car2 = Car.createObj("TATA", 4, 5);
System.out.println(car2.name);
// System.out.println(car2.wheelCount );
System.out.println(car2.seatCount);
}
}
class Car {
static Car obj;
int wheelCount;
int seatCount;
String name;
// new Car()
private Car(String nam, int wheelCount, int seatCount) {
name = nam;
this.wheelCount = wheelCount;
this.seatCount = seatCount;
}
static Car createObj(String name, int wheelCount, int seatCount) {
if (obj == null) {
obj = new Car(name, wheelCount, seatCount);
}
return obj;
}
}