-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalInnerClass.java
More file actions
41 lines (39 loc) · 1.08 KB
/
Copy pathLocalInnerClass.java
File metadata and controls
41 lines (39 loc) · 1.08 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
interface Counter{
int next();
}
public class LocalInnerClass{
private int count = 0;
Counter getCounter(final String name){
class LocalCounter implements Counter{
public LocalCounter(){
System.out.println("LocalCounter()");
}
public int next(){
System.out.print(name);
return count++;
}
}
return new LocalCounter();
}
Counter getCounter2(final String name){
return new Counter(){
{
System.out.println("Counter()");
}
public int next(){
System.out.print(name);
return count++;
}
};
}
public static void main(String[] args){
LocalInnerClass lic = new LocalInnerClass();
Counter c1 = lic.getCounter("Local inner "), c2=lic.getCounter2("Anonymous inner ");
for(int i=0;i<5;i++){
System.out.println(c1.next());
}
for(int i=0;i<5;i++){
System.out.println(c2.next());
}
}
}