Let's consider following code:
public class MethodTest_Failure1 {
static class MethodSupplier {
private void m() {
System.out.println("Hi");
}
}
static class MethodInvoker extends MethodSupplier {
public void invoke() {
this.m();
}
}
public static void main(String[] args) {
new MethodInvoker().invoke();
}
}
Its compilation failed with following error:
java: cannot find symbol
symbol: method m()
However according to following assertion from "6.6.1. Determining Accessibility" the method should be accessible:
Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
I would also like to notice that after casting 'this' to MethodSupplier the compilation succeeds:
public class MethodTest_Ok1 {
static class MethodSupplier {
private void m() {
System.out.println("Hi");
}
}
static class MethodInvoker extends MethodSupplier {
public void invoke() {
((MethodSupplier)this).m();
}
}
public static void main(String[] args) {
new MethodInvoker().invoke();
}
}
The same situation takes place for:
- method references;
- when inner class extends its enclosing class;
- when making invocation without 'this' qualifier.
The minimized tests are attached for all these cases.
This was reproduced on Windows7x64 platform for:
- JDK 8 b102 x64bit;
- JDK 7u7 x64bit (just invoking methods);
public class MethodTest_Failure1 {
static class MethodSupplier {
private void m() {
System.out.println("Hi");
}
}
static class MethodInvoker extends MethodSupplier {
public void invoke() {
this.m();
}
}
public static void main(String[] args) {
new MethodInvoker().invoke();
}
}
Its compilation failed with following error:
java: cannot find symbol
symbol: method m()
However according to following assertion from "6.6.1. Determining Accessibility" the method should be accessible:
Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
I would also like to notice that after casting 'this' to MethodSupplier the compilation succeeds:
public class MethodTest_Ok1 {
static class MethodSupplier {
private void m() {
System.out.println("Hi");
}
}
static class MethodInvoker extends MethodSupplier {
public void invoke() {
((MethodSupplier)this).m();
}
}
public static void main(String[] args) {
new MethodInvoker().invoke();
}
}
The same situation takes place for:
- method references;
- when inner class extends its enclosing class;
- when making invocation without 'this' qualifier.
The minimized tests are attached for all these cases.
This was reproduced on Windows7x64 platform for:
- JDK 8 b102 x64bit;
- JDK 7u7 x64bit (just invoking methods);