So most of us know how to access an outer class from an inner class. Searches with those words give oodles of answered questions on that topic. But what I want to know is why the syntax is the way it is.
Example:
public class A
{
private class B
{
public void c()
{A.this.d();}
public void d()
{System.out.println("You called the d() in the B class! Oh noes!");}
}
public void d()
{System.out.println("You've called d()! Go, you!");}
}
Why is it A.this.d()? It looks like this is a static field of class A, but… * am confused *
Forgive me if this is a repeat; like I said, searches with those words give how-answers.
Why is it done that way? Really, it’s just because of the way it is. It works, it sort of makes sense, and there’s no need to come up with fancy syntax to do the job.
When you see something like this:
The
.can mean a lot of things:In other words, the
.is overloaded to serve many grammatical functions within Java programming language. It may lead to confusion, and it can be problematic, but that’s just the way it is.It helps to follow naming convention, but certain things can still look ambiguous and/or confusing.
See also