-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticModifierExamples.java
84 lines (66 loc) · 2.2 KB
/
StaticModifierExamples.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
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
package concept.examples.membermodifiers.nonaccess;
class Cricketer {
private static int count;
public Cricketer() {
count++;
}
static int getCount() {
return count;
}
}
class Animal {
static void StaticMethod() {
System.out.println("Animal Static Method");
}
}
class Dog extends Animal {
static void StaticMethod() {
System.out.println("Dog Static Method");
}
}
public class StaticModifierExamples {
private static int staticVariable;
private int instanceVariable;
public static void staticMethod() {
// instance variables are not accessible in static methods
// instanceVariable = 10; //COMPILER ERROR
// Also, this Keyword is not accessible.this refers to object.
// static methods are class methods
// static variables are accessible in static methods
staticVariable = 10;
}
public void instanceMethod() {
// static and instance variables are accessible in instance methods
instanceVariable = 10;
staticVariable = 10;
}
public static void main(String[] args) {
// static int i =5; //COMPILER ERROR
StaticModifierExamples example = new StaticModifierExamples();
// instance variables and methods are only accessible through object
// references
example.instanceVariable = 10;
example.instanceMethod();
// StaticModifierExamples.instanceVariable = 10;//COMPILER ERROR
// StaticModifierExamples.instanceMethod();//COMPILER ERROR
// static variables and methods are accessible through object references
// and Class Name.
StaticModifierExamples.staticVariable = 10;
StaticModifierExamples.staticMethod();
StaticModifierExamples.staticVariable = 10;
StaticModifierExamples.staticMethod();
// It is always recommended to use Class Name to access a static
// variable or method.
// This is because static methods are class level methods. It is not
// appropriate
// to use instance references to call static methods (even though it
// compiles and works).
Cricketer cricketer1 = new Cricketer();
Cricketer cricketer2 = new Cricketer();
Cricketer cricketer3 = new Cricketer();
Cricketer cricketer4 = new Cricketer();
System.out.println(Cricketer.getCount());// 4
Animal animal = new Dog();
animal.StaticMethod();// Animal Static Method
}
}