The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

Advertisement

Advertisement

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:

Maybe an Answer

Posted by Ivan Villanueva on January 19, 2001 at 9:12 AM

It seems, that all new data in a anonymous inner class (no overridden from the superclass) are private?
I try something and it works:

package delete;

class C1 {
int numberC1 = 1 ;
}
class C2 {
int numberC2 = 2 ;
int prn() { return numberC2 ; }
}

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(){
public int numberC2IA ;
{ numberC2IA = 5 ; }
int prn() { return numberC2IA ; }
} ;
// This access the datamember numberC2IA of the anonymous inner class:
void proof() { System.out.println(c2IAObject.prn()) ; }
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()) ;
m.proof() ; // it prints data of the anonymous inner class.

// 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:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us