-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstatic_f_106_hiding.java
More file actions
129 lines (107 loc) · 3.2 KB
/
Copy pathstatic_f_106_hiding.java
File metadata and controls
129 lines (107 loc) · 3.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Static field hiding:
We have 6 modes to access a static field:
- unqualified f in static context
- unqualified f in instance context
- this.f
- object.f
- Class.f
- super.f
We have three hiding scenarios involving static fields:
- static hides static field
- static hides instance
- instance hides static
We should test every combination that passes static checking.
*/
public class static_f_106_hiding {
public static void main(String[] args) {
A.a = 1;
A.c = 3;
B.a = 4;
B.b = 5;
C c = new C();
System.out.println("Access fields in the context of class A:");
c.staticTestA();
c.instTestA();
c.thisQTestA();
System.out.print("a.field: ");
System.out.println("a=" + ((A)c).a + " b=" + ((A)c).b + " c=" + ((A)c).c);
System.out.print("A.field: ");
System.out.println("a=" + A.a + " c=" + A.c);
System.out.println("\nAccess fields in the context of class B:");
c.staticTestB();
c.instTestB();
c.thisQTestB();
System.out.print("b.field: ");
System.out.println("a=" + ((B)c).a + " b=" + ((B)c).b + " c=" + ((B)c).c);
System.out.print("B.field: ");
System.out.println("a=" + B.a + " b=" + B.b);
c.superQTestB();
System.out.println("\nAccess fields in the context of class C:");
c.staticTestC();
c.instTestC();
c.thisQTestC();
System.out.print("c.field: ");
System.out.println("a=" + c.a + " b=" + c.b + " c=" + c.c);
System.out.print("C.field: ");
System.out.println("a=" + C.a + " b=" + C.b);
c.superQTestC();
System.out.println("Done!");
}
}
class A {
static int a;
int b = 2;
static int c;
void instTestA() {
System.out.print("(inst A): ");
System.out.println("a=" + a + " b=" + b + " c=" + c);
}
static void staticTestA() {
System.out.print("(static A)field:");
System.out.println("a=" + a + " c=" + c);
}
void thisQTestA() {
System.out.print("(A)this.field: ");
System.out.println("a=" + this.a + " b=" + this.b + " c=" + this.c);
}
}
class B extends A {
static int a;
static int b;
int c = 6;
void instTestB() {
System.out.print("(inst B)field: ");
System.out.println("a=" + a + " b=" + b + " c=" + c);
}
static void staticTestB() {
System.out.print("(static B)field:");
System.out.println("a=" + a + " b=" + b);
}
void thisQTestB() {
System.out.print("(B)this.field: ");
System.out.println("a=" + this.a + " b=" + this.b + " c=" + this.c);
}
void superQTestB() {
System.out.print("(B)super.field: ");
System.out.println("a=" + super.a + " b=" + super.b + " c=" + super.c);
}
}
class C extends B {
void instTestC() {
System.out.print("(inst C)field: ");
System.out.println("a=" + a + " b=" + b + " c=" + c);
}
static void staticTestC() {
System.out.print("(static C)field:");
System.out.println("a=" + a + " b=" + b);
}
void thisQTestC() {
System.out.print("(C)this.field: ");
System.out.println("a=" + this.a + " b=" + this.b + " c=" + this.c);
}
void superQTestC() {
System.out.print("(C)super.field: ");
System.out.println("a=" + super.a + " b=" + super.b + " c=" + super.c);
}
}