Why does Java allow such strange code using inner class to pass compilation?
Hi I tried below code while learning Java inner class. So surprising it passed compilation but could not work in run-time. My understanding is for inner class it must be instantiated in an instance of the top class开发者_如何学C.
But why JDK compiler allow such code to pass compilation? I am using JDK 6.
public class Hello
{
public Hello()
{
System.out.println("Simple Hello!");
}
public void test()
{
Test.test();
}
protected int i = 0;
static class B
{
public B()
{
System.out.println("B Hello!");
}
static class C
{
public C()
{
System.out.println("C Hello!");
}
}
}
}
class Test
{
static void test()
{
C c = new C();
}
}
Here’s what I get:
$ javac -version
javac 1.6.0_26
$ javac Hello.java
Hello.java:31: cannot find symbol
symbol : class C
location: class Test
C c = new C();
^
Hello.java:31: cannot find symbol
symbol : class C
location: class Test
C c = new C();
^
2 errors
Are you 100% sure you are able to compile that code?
精彩评论