forked from tugul/CoreJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticMember.java
More file actions
35 lines (29 loc) · 922 Bytes
/
Copy pathStaticMember.java
File metadata and controls
35 lines (29 loc) · 922 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
package classes;
/**
* Static members can be accessed not only through class name,
* but also through instance of the class.
* Even when reference of the instance points to NULL
*
* But instance members can't be accessed if reference points to NULL
*/
class A {
static int staticField;
int instanceField;
public static void main(String[] args) {
System.out.println(staticField);
}
}
public class StaticMember {
public static void main(String[] args) {
A.main(new String[0]); // 0
A a = new A();
System.out.println(a.staticField); // 0
a.instanceField = 100;
a = null;
System.out.println(a.staticField); // 0
System.out.println(a.instanceField); // NullPointerException
A b;
System.out.println(b.staticField); // 0
System.out.println(A.staticField); // 0
}
}