Explanation could be extended to:To access Onion's data inside Layer, you can do Onion.this.data.
To access Onion's data inside Layer, you can do Onion.this.data or super.data.
Moderator: admin
Explanation could be extended to:To access Onion's data inside Layer, you can do Onion.this.data.
To access Onion's data inside Layer, you can do Onion.this.data or super.data.
What is the reasoning behind this method call? How can the innerclass access outer class's instance method? Accessing outer class variables is fine! But this is something out of the worlD! And that too without returning an actual String it still compiles!admin wrote:If Layer's getData is not present, Onion's getData() will be invoked, which calls return new Layer().getData(); This is basically an unending recursive call. Hence the error.
Why do you think accessing outer class variables is different from accessing outer class methods? Both are members. Access rules should apply equally to both.jagoneye wrote:What is the reasoning behind this method call? How can the innerclass access outer class's instance method? Accessing outer class variables is fine! But this is something out of the worlD! And that too without returning an actual String it still compiles!admin wrote:If Layer's getData is not present, Onion's getData() will be invoked, which calls return new Layer().getData(); This is basically an unending recursive call. Hence the error.
Layer class can access getData() method of Onion class because Layer is extending Onion, thus it inherits getData(), but not because it is an inner class of Onion. Actually, if you remove getData() from Layer and the "extends Onion" from Layer class definition, the code will not compile.admin wrote:Why do you think accessing outer class variables is different from accessing outer class methods? Both are members. Access rules should apply equally to both.jagoneye wrote:What is the reasoning behind this method call? How can the innerclass access outer class's instance method? Accessing outer class variables is fine! But this is something out of the worlD! And that too without returning an actual String it still compiles!admin wrote:If Layer's getData is not present, Onion's getData() will be invoked, which calls return new Layer().getData(); This is basically an unending recursive call. Hence the error.
admin wrote:>>new Layer().getData(); // "Cannot resolve method getData()"
This will indeed not compile because Layer's doesn't have getData. Onion Does. Layer can access Onion's getData. So the following compiles even if you remove extends Onion and getData method from Layer -
public void getX() {
System.out.println(getData()); //uses Onion's getData
}
Users browsing this forum: No registered users and 45 guests