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
31 lines (26 loc) · 806 Bytes
/
Copy pathStaticMember.java
File metadata and controls
31 lines (26 loc) · 806 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
package classes;
/**
* Static members can be accessed not only through class name,
* but also through instance of the class.
* Even after reference of the instance points to NULL
*
* But instance members can't be accessed if reference is 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
}
}