Download Shadow
/* This demonstrates shadowing for nested and inner classes.
* The commented out lines would cause errors; can you see why?
*
* Written 3/2006 by Wayne Pollock, Tampa Florida USA.
*/
class Outer
{
int num = 1;
static int staticNum = 1;
class Inner
{
int num = 2;
void aMethod ()
{
System.out.println( "num=" + num );
System.out.println( "this.num=" + this.num );
System.out.println( "Inner.this.num=" + Inner.this.num );
System.out.println( "Outer.this.num=" + Outer.this.num );
System.out.println( "\nstaticNum=" + staticNum );
// System.out.println( "this.staticNum=" + this.staticNum );
// System.out.println( "Inner.this.staticNum=" + Inner.this.staticNum );
System.out.println( "Outer.this.staticNum=" + Outer.this.staticNum );
System.out.println( "Outer.staticNum=" + Outer.staticNum );
}
}
static class Nested
{
int num = 3;
void aMethod ()
{
System.out.println( "num=" + num );
System.out.println( "this.num=" + this.num );
System.out.println( "Nested.this.num=" + Nested.this.num );
// System.out.println( "Outer.this.num=" + Outer.this.num );
System.out.println( "\nstaticNum=" + staticNum );
System.out.println( "Outer.staticNum=" + Outer.staticNum );
// System.out.println( "Nested.staticNum=" + Nested.staticNum );
// System.out.println( "this.staticNum=" + this.staticNum );
// System.out.println( "Nested.this.staticNum=" + Nested.this.staticNum );
// System.out.println( "Outer.this.staticNum=" + Outer.this.staticNum );
}
}
}
public class Shadow
{
public static void main ( String [] args )
{
Outer.Nested nested = new Outer.Nested();
System.out.println( "\n\tInvoking nested.aMethod():\n" );
nested.aMethod();
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
System.out.println( "\n\tInvoking inner.aMethod():\n" );
inner.aMethod();
}
}