-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathConstEx1.java
46 lines (35 loc) · 921 Bytes
/
ConstEx1.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
39
40
41
42
43
44
45
46
package Constructor;
public class ConstEx1 {
// 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.
int x, y;
// no-argument constructor
ConstEx1() {
getX(); // we can call both static and non-static method from
// constructor
x = 0;
y = 0;
}
// Parameterized Constructor
ConstEx1(int l) {
x = y = l;
getX();
}
ConstEx1(int l, int m) {
x = l;
y = m;
}
static void getX() {
System.out.println(" You're inside method getX() ");
// return x;
}
public static void main(String[] args) {
ConstEx1 c1 = new ConstEx1();
System.out.println("[C1]X= " + c1.x + " Y= " + c1.y);
ConstEx1 c2 = new ConstEx1(20);
ConstEx1 c3 = new ConstEx1(12, 13);
System.out.println("[C2]X= " + c2.x + " Y= " + c2.y);
System.out.println("[C3]X= " + c3.x + " Y= " + c3.y);
}
}