开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜