public class OuterClass {
public int x = 42;
public void method1() {
class LocalClass {
public void localPrint() {
System.out.println("In local class");
System.out.println(x);
}
}
LocalClass lc = new LocalClass();
lc.localPrint();
}
public void method2() {
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("In an anonymous local class method");
System.out.println(x);
}
};
r.run();
}
public Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("In an anonymous class method");
System.out.println(x);
}
};
Object o = new Object() {
@Override
public String toString() {
return "In an anonymous class method";
}
};
public class InnerClass {
public static final int y = 44;
public void innerPrint() {
System.out.println("In a inner class method");
System.out.println(x);
}
}
public class NestedClassesMain {
public static void main(String[] args) {
OuterClass co = new OuterClass();
co.method1();
co.method2();
co.r.run();
OuterClass.InnerClass i = co.new InnerClass();
i.innerPrint();
OuterClass.StaticNestedClass sn = new OuterClass.StaticNestedClass();
sn.staticNestedPrint();
OuterClass.A.B nested = co.new A().new B();
}
}
No comments:
Post a Comment