Inner classes have access to enclosing class variables.
public class Outer {
private int foo;
class Inner {
private int bar;
Inner() {
this.bar = foo+1;
}
}
}
What if we want to rename bar to foo as well? "this" prefix will refer to inner class so we have to qualify it with outer class as below.
this.bar = foo+1;
}
}
}
What if we want to rename bar to foo as well? "this" prefix will refer to inner class so we have to qualify it with outer class as below.
public class Outer {
private int foo;
class Inner {
private int foo;
Inner() {
this.foo = Outer.this.foo+1;
}
}
}
this.foo = Outer.this.foo+1;
}
}
}
No comments:
Post a Comment