Tuesday, 17 October 2017

6 Local inner classes

1. How to create an object for Local Inner class?
  1. Local inner class can define in any block or method either static or non static (within loops or expressions also possible)
  2. Local inner class can have only instance members, no static members for except in below condition
  3. Local inner class can contains final static variables, not methods
  4. Local inner class can have only instance methods, blocks
  5. Local inner class can access outer class static, non static members if and only if block or method is non static
  6. Local inner class can access outer class static members only if and only if block or method is static
  7. 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

3. Java Program to create Binary Tree