-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructors.java
More file actions
58 lines (51 loc) · 1.83 KB
/
Constructors.java
File metadata and controls
58 lines (51 loc) · 1.83 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
58
//Constructors= They are used to create objects in java
/* properties: Must have same name that class have
constructs not used any return type while initializing like int float
Only calls one time when object creates */
class Vehicle {
String name;
int mileage;
int price;
String color;
Vehicle() {
System.out.println("Vehicle Details:"); //Non parameterized constructor
}
Vehicle(int price, String color) //Parameterized Constructor
{
this.price = price;
this.color = color;
} //Why java not have destructors to destroy the constructos
} //that not in use:
//bcoz Java have Garbage collectors inbuilt that destroy the objects tha not in use
public class Constructors {
public static void main(String... s) {
Vehicle v = new Vehicle(77, "Black");
v.name = "Mercedes";
v.mileage = 7;
System.out.println(v.name);
System.out.println(v.mileage);
System.out.println(v.price);
System.out.println(v.color);
}
}
//Another way to attain Parameterized constructor
// class Student {
// String name;
// char section;
// Student(String name, char section) {
// this.name = name;
// this.section = section;
// }
// public void show() {
// System.out.println(this.name);
// System.out.println(this.section);
// }
// }
// public class Constructors {
// public static void main(String[] args) {
// Student s1 = new Student("Kushant", 'B');
// Student s2 = new Student("Diksha", 'C');
// s1.show();
// s2.show();
// }
// }