When referencing the outer class from the inner class External class name.this There seems to be a notation of, so I did a little research.
First, I came up with
public class Main {
public static void main(String []args) {
TestThis t = new TestThis();
t.this.hp = 10;///
}
public class TestThis {
int hp;
int mp;
String name;
}
}
I wondered if this was the case
t.this.hp = 10;
This part is eclipse, and t cannot be resolved into a mold. Do you want to create a t class? Came out.
If t.hp = 10 ;, what's ok?
Apparently, the interpretation of the inner class was wrong,
class out~{ class in~{ } }
Image of. Based on this, experiment again with Eclipse.
public class Main {
public static void main(String []args) {
Outer o = new Outer();
Outer.Inner oi = o.new Inner();
oi.innerPrint();
}
public class Outer{
int outerhp = 10;
public class Inner{
public void innerPrint() {
System.out.println(Outer.this.outerhp);
}
}
}
}
Still an error According to Eclipse There is no accessible instance of type Main. You must limit the allocation on an enclosing instance of type Main (for example, x.new A (), where x is an instance of Main).
When I looked it up, I didn't understand a little.
public class Main {
public static void main(String []args) {
Outer o = new Outer();
Outer.Inner oi = o.new Inner();
oi.innerPrint();
}
public static class Outer{
int outerhp = 10;
public class Inner{
public void innerPrint() {
System.out.println(Outer.this.outerhp);
}
}
}
}
From the result, it works without error. (Outer class is static)
It looks like something close to a bug, so I'd like to investigate it tomorrow. http://d.hatena.ne.jp/chiheisen/20110502/1304272928 Looking at the position of the indent, I feel like I'm saying something different from my code. main (String [] args) is in Outer ... I have never written such a code.
Outer.Inner oi = o.new Inner();
I think I saw this new way in a book before. Once again, you can read that book with a tree stump on the cover.
Every time I correct the indentation, it shifts lol I wonder if the format is different if I pasted it from Eclipse.