-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSingleTon.java
38 lines (35 loc) · 1.23 KB
/
SingleTon.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
public class SingleTon {
public static void main(String[] args) {
// Car car2 = new Car(4, "Toyota", "Blue", "Diesel");
Car car1 = Car.createObj(4, "Toyota", "Blue", "Diesel");
System.out.println(car1.wheelCount);
System.out.println(car1.color);
System.out.println(car1.brandName);
System.out.println(car1.fuelType);
System.out.println("****************");
Car car2 = Car.createObj(4, "TATA", "RED", "Diesel");
System.out.println(car2.wheelCount);
System.out.println(car2.color);
System.out.println(car2.brandName);
System.out.println(car2.fuelType);
}
}
class Car {
static Car obj;
int wheelCount;
String brandName;
String color;
String fuelType;
private Car(int wheelCount, String brandName, String color, String fuelType) {
this.wheelCount = wheelCount;
this.brandName = brandName;
this.color = color;
this.fuelType = fuelType;
}
static Car createObj(int wheelCount, String brandName, String color, String fuelType) {
if (obj == null) {
obj = new Car(wheelCount, brandName, color, fuelType);
}
return obj;
}
}