|
Access the enclosing class from an inner class |
|
|
The following code snippets show how to access the enclosing class from an inner class.
public class TestIt {
public static void main(String a[]){
new TestIt().doit();
}
public void doit() {
new InnerClass().sayHello();
}
public void enclosingClassMethod(){
System.out.println("Hello world!");
}
class InnerClass {
public void sayHello() {
TestIt.this.enclosingClassMethod();
}
}
}
or
public class TestIt {
TestIt testItClass = this;
public static void main(String a[]){
new TestIt().doit();
}
public void doit() {
new InnerClass().sayHello();
}
public void enclosingClassMethod(){
System.out.println("Hello world!");
}
class InnerClass {
public void sayHello() {
testItClass.enclosingClassMethod();
}
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.