- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstEx1.java
More file actions
Latest commit
46 lines (35 loc) · 921 Bytes
/
Copy pathConstEx1.java
File metadata and controls
46 lines (35 loc) · 921 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
36
37
38
39
40
41
42
43
44
45
46
packageConstructor;
publicclassConstEx1 {
// if we won't provide any constructor compiler adds default constructor
// int this case, ConstEx1(){} , but if we provide any other constructor
// compiler won't add.
intx, y;
// no-argument constructor
ConstEx1() {
getX(); // we can call both static and non-static method from
// constructor
x = 0;
y = 0;
}
// Parameterized Constructor
ConstEx1(intl) {
x = y = l;
getX();
}
ConstEx1(intl, intm) {
x = l;
y = m;
}
staticvoidgetX() {
System.out.println(" You're inside method getX() ");
// return x;
}
publicstaticvoidmain(String[] args) {
ConstEx1c1 = newConstEx1();
System.out.println("[C1]X= " + c1.x + " Y= " + c1.y);
ConstEx1c2 = newConstEx1(20);
ConstEx1c3 = newConstEx1(12, 13);
System.out.println("[C2]X= " + c2.x + " Y= " + c2.y);
System.out.println("[C3]X= " + c3.x + " Y= " + c3.y);
}
}