Repository navigation
Expand file tree
/
Copy pathInnerClass.java
More file actions
100 lines (83 loc) · 2.93 KB
/
Copy pathInnerClass.java
File metadata and controls
100 lines (83 loc) · 2.93 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
package com.object;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
public class InnerClass {
public static AtomicInteger counter = new AtomicInteger(1);
private String level;
/**
* As a side note, we can’t have static method in a nested inner class
* because an inner class is implicitly associated with an object of its outer class
* so it cannot define any static method for itself.
*/
class NestedInnerClass {
public static final String NAME = "common inner ";
public void echo() {
System.out.println(NAME + counter.getAndIncrement());
}
}
public void innerClassInMethod() {
System.out.println("inside a method");
int age = 10;
String[] location = {"in method"};
class MethodLocalInnerClass {
private String name = "MethodLocalInnerClass ";
public void echo() {
System.out.println(name + counter.getAndIncrement());
level = "method level";
System.out.println("out level is: " + level);
/**
* Local inner class cannot access non-final local variable till JDK 1.7. Since JDK 1.8,
* it is possible to access the non-final local variable in method local inner class.
*
* but could not modify it, like age++
*/
System.out.println(age);
/**
* could change the value in the object though
*/
location[0] = "inside";
System.out.println(Arrays.toString(location));
}
}
MethodLocalInnerClass methodLocalInnerClass = new MethodLocalInnerClass();
methodLocalInnerClass.echo();
}
/**
* Static nested classes are not technically an inner class.
* They are like a static member of outer class.
*/
static class StaticNestedClass {
public void echo() {
System.out.println("StaticNestedClass");
}
}
class Demo {
void show() {
System.out.println("i am in show method of super class");
}
}
public void anonyInnerClass() {
Demo demo = new Demo(){
void show() {
super.show();
System.out.println("in anony innner class");
}
};
demo.show();
Hello hello = new Hello() {
@Override
public void hi() {
System.out.println("hello from anony.");
}
};
hello.hi();
}
public static void main(String[] args) {
InnerClass innerClass = new InnerClass();
innerClass.new NestedInnerClass().echo();
innerClass.innerClassInMethod();
InnerClass.StaticNestedClass staticNestedClass = new StaticNestedClass();
staticNestedClass.echo();
innerClass.anonyInnerClass();
}
}