-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInnerClassExamples.java
99 lines (75 loc) · 2.95 KB
/
InnerClassExamples.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package concept.examples.innerclass;
class OuterClass {
private int outerClassInstanceVariable;
public void exampleMethod() {
int localVariable;
final int finalVariable = 5;
class MethodLocalInnerClass {
public void method() {
// Can access class instance variables
System.out.println(outerClassInstanceVariable);
// Cannot access method's non-final local variables
// localVariable = 5;//Compiler Error
System.out.println(finalVariable);// Final variable is fine..
}
}
// MethodLocalInnerClass can be instantiated only in this method
MethodLocalInnerClass m1 = new MethodLocalInnerClass();
m1.method();
}
// MethodLocalInnerClass can be instantiated only in the method where it is
// declared
// MethodLocalInnerClass m1 = new MethodLocalInnerClass();//COMPILER ERROR
public static class StaticNestedClass {
private int staticNestedClassVariable;
public int getStaticNestedClassVariable() {
return staticNestedClassVariable;
}
public void setStaticNestedClassVariable(int staticNestedClassVariable) {
this.staticNestedClassVariable = staticNestedClassVariable;
}
public void privateVariablesOfOuterClassAreNOTAvailable() {
// outerClassInstanceVariable = 5; //COMPILE ERROR
}
}
public class InnerClass {
private int innerClassVariable;
public int getInnerClassVariable() {
return innerClassVariable;
}
public void setInnerClassVariable(int innerClassVariable) {
this.innerClassVariable = innerClassVariable;
}
public void privateVariablesOfOuterClassAreAvailable() {
outerClassInstanceVariable = 5; // we can access the value and
// change it
System.out.println("Inner class ref is " + this);
// Accessing outer class reference variable
System.out.println("Outer class ref is " + OuterClass.this);
}
}
public void createInnerClass() {
// Just use the inner class name to create it
InnerClass inner = new InnerClass();
}
}
public class InnerClassExamples {
public static void main(String[] args) {
// Static Nested Class can be created without needing to create its
// parent. Without creating NestedClassesExample, we created
// StaticNestedClass
OuterClass.StaticNestedClass staticNestedClass1 = new OuterClass.StaticNestedClass();
staticNestedClass1.setStaticNestedClassVariable(5);
OuterClass.StaticNestedClass staticNestedClass2 = new OuterClass.StaticNestedClass();
staticNestedClass2.setStaticNestedClassVariable(10);
// Static Nested Class member variables are not static. They can have
// different values.
System.out.println(staticNestedClass1.getStaticNestedClassVariable()); // 5
System.out.println(staticNestedClass2.getStaticNestedClassVariable()); // 10
// COMPILER ERROR! You cannot create an inner class on its own.
// InnerClass innerClass = new InnerClass();
OuterClass example = new OuterClass();
// To create an Inner Class you need an instance of Outer Class
OuterClass.InnerClass innerClass = example.new InnerClass();
}
}