我的文件名是Srwhile
public class Srwhile  //如果Srwhile改成aa,就会出错,那就必须把public去掉.
{
public static void main(String args[])
{
int n=1;
while(n<=5)
{
System.out.println("欢迎");
n++;
}
}
}
很不理解为什么文件名也与编译有关????

解决方案 »

  1.   

    public class名字一定要和文件名一样
      

  2.   

    没错··~
    一个文件只有一个public类~~
      

  3.   

    一个.java的源文件里只有一个public修饰的类,则这个类的名字必须是改源文件的名字.
      

  4.   

    今天钻牛角尖的人还真多,规范上这样写的。When packages are stored in a file system (§7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
    The type is referred to by code in other compilation units of the package in which the type is declared. 
    The type is declared public (and therefore is potentially accessible from code in other packages). This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.
    When packages are stored in a database (§7.2.2), the host system must not impose such restrictions. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.参考
    http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.7
      

  5.   


    只要知道如果类是public 的话文件名必须与其一致....,规定,
      

  6.   

    别人规定这样  就这样撒
      用别人的东西 就要遵守别人的规定   
          就像这里写的  
     “ 这里发贴,表示您接受了CSDN社区的 用户行为 准则。
    请您对您的言行负责,并遵守中华人民共和国有关法律、法规,尊重网上道德。 ”
      

  7.   

    因为你写的Srwhile类已经编译过了,你将Srwhile改名为aa,编译器找不到你原来定义的类,所以会报错
    不知道你用的什么开发环境,如果是Eclipse,那么在类名处,按ALT+SHIFT+R,可以改名,编译器会重新编译
    不知道说的对不对,自己再试试,用得久了,自然就明白怎么回事了,有时候不见得是真懂,成习惯后就是懂了,要想究深层原因,储备知识(即已形成的习惯)很重要得