1. How to create an object for Local Inner class?
- Local inner class can define in any block or method either static or non static (within loops or expressions also possible)
- Local inner class can have only instance members, no static members for except in below condition
- Local inner class can contains final static variables, not methods
- Local inner class can have only instance methods, blocks
- Local inner class can access outer class static, non static members if and only if block or method is non static
- Local inner class can access outer class static members only if and only if block or method is static
- Local inner class can be accessed in within the outer class method or block scope only
public class OuterClass {
int i = 10;
static int j = 20;
void test() {
class InnerClass {
int i = 100;
static final int j = 200;
void innerTest() {
System.out.println("I am in test InnerClass " + i);
System.out.println("I am in test OuterClass " + OuterClass.this.i);
System.out.println("I am in test OuterClass " + OuterClass.j);
}
}
System.out.println(InnerClass.j);
System.out.println(new InnerClass().i);
new InnerClass().innerTest();
}
public static void main(String[] args) {
new OuterClass().test();
}
}
|
No comments:
Post a Comment