This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Accessing to data in anonymous inner classes
Posted by Ivan Villanueva on January 19, 2001 at 8:08 AM
I don't know why I cannot access to the anonymous inner class data like I access to a normal inner class data. Hier is the example with a normal inner class data (it works) and a anonymous inner class data (it doesn't work): package delete; abstract class C1 { int numberC1 = 1 ; } class C2 { int numberC2 = 2 ; } public class multipleI extends C1 { static void takeC1(C1 c1) {} static void takeC2(C2 c2) {} int numberMultipleI = 3 ; class C2b extends C2 { int numberC2b = 4 ; int function() { return numberC1 + numberC2 + numberMultipleI + numberC2b ; } } C2b c2bObject = new C2b() ; C2 c2IAObject = new C2(){ int numberC2IA ; { numberC2IA = 5 ; } int newFunctionInAnonymousInnerClass() { return numberC2IA ; } } ; public static void main (String args[]) { multipleI m = new multipleI() ; takeC1(m) ; takeC2(m.c2bObject) ; System.out.println(m.c2bObject.numberC2b) ; System.out.println(m.c2bObject.function()) ; // This doesn't work. Why ??!! // System.out.println(m.c2IAObject.numberC2IA) ; // System.out.println(m.c2IAObject.newFunctionInAnonymousInnerClass()) ; // System.out.println(m.c2bObject.numberC1) ; // System.out.println(m.c2bObject.numberMultipleI) ; } }
Replies:
|