现在有个Java元文件 名为:Foo.java
class Foo{
Bar b;
};
class Bar{
Foo a;
};我直接编译 javac Foo.java 居然编译过去了,
这正常吗 ?
记得上学的时候  树上好像写的最起码要是如下样子的才能编译吧public class Foo{
Bar b;
};
class Bar{
Foo a;
};

解决方案 »

  1.   

    foo这个类是public,一个文件中只可以有一个public的类,这个类的名字恰恰也是文件的名字。所以bar是一个内部类,内部类和外部类之间可以访问的。所以不会出现错误。
      

  2.   

    The class "Bar" is not inside the class "Foo", so it is not the inner class, but the parallel class to Foo. Thus, it is also why we can not declare the "Bar" class as "public" because the fist layer public class should be declared in his own file. And if you declare two parallel classes as "public", the JVM would not know which source file it belongs to, it is to say that the Java compiler would throw errors just because of the ambiguity.
      

  3.   

    public类只是说与其同名的文件内只能有一个,并不是说一定要有public类不可。
      

  4.   

    没有public类的,文件名没有限制,
    有public类的则只能有一个public类并且要与文件名相同。