简体   繁体   English

java中的文件名和类名不同

[英]File name and class name different in java

I created a file named In.java an entered the following code 我创建了一个名为In.java的文件,输入以下代码

class In {
   int a;

   public static void main(
      String args[] ) {
      Inheritance h = new Inheritance();
      h.set( 6 );
      System.out.println( h.get() );
   }

   void set(
      int a ) {
      this.a = a;
   }

   int get() {
      System.out.println( a );
      return a;
   }
}

When I compiled it showed me an error regarding Inheritance. 编译时,它向我显示了有关继承的错误。 Then I renamed In as I Inheritance 然后我将In重命名为In I Inheritance

class Inheritance {
   int a;

   public static void main(
      String args[] ) {
      Inheritance h = new Inheritance();
      h.set( 6 );
      System.out.println( h.get() );
   }

   void set(
      int a ) {
      this.a = a;
   }

   int get() {
      System.out.println( a );
      return a;
   }
}

Now when I compiled it compiled and created Inheritance.class but the file name was In.java still when I compile as public class Inheritance it alerts me that the class should be changed as Inheritance.java . 现在当我编译它时编译并创建了Inheritance.class,但是当我编译为public class Inheritance时,文件名仍然是In.java,它提醒我应该将类更改为Inheritance.java。 now when I run java In it shows an error as Error: Could not find or load main class In Now I again renamed the class as In as 现在当我运行java In它显示错误为Error: Could not find or load main class In现在我再次将该类重命名为In as

class In {
   int a;

   public static void main(
      String args[] ) {
      Inheritance h = new Inheritance();
      h.set( 6 );
      System.out.println( h.get() );
   }

   void set(
      int a ) {
      this.a = a;
   }

   int get() {
      System.out.println( a );
      return a;
   }
}

Now when I compiled it compiled as In.class and when I run the output it had run the program showing 现在,当我编译它编译为In.class时,当我运行输出它运行程序显示

6 6 6 6

When I created the program with In.java and run the class with name as Class Inheritance it compiled and gave the Inheritance.class . 当我用In.java创建程序并运行名为Class Inheritance的类时,它编译并给出了Inheritance.class。 1. If the class name and file name are different won't the compiler show up an error? 1.如果类名和文件名不同,编译器是否会显示错误? 2. when I run the java In it shows Error: Could not find or load main class In as the In.class file is generated Why doesn't it detect it while compiling In.java with class name as Inheritance ? 2.当我运行java In它显示Error: Could not find or load main class In生成In.class文件为什么在编译带有类名作为继承的In.java时它没有检测到它? so can one class file use any other class file in the same directory? 那么一个类文件可以在同一目录中使用任何其他类文件吗?

Any class which is declared public should be kept in a file of the same name. 任何声明为public类都应保存在同名文件中。 It is a compilation error if the names of the public class and the file which contains it are different. 如果公共类的名称和包含它的文件不同,则会出现编译错误。 A class which isn't declared public can be kept in a file of a different name. 未声明为public可以保存在不同名称的文件中。

Note that the generated class files are named after the java classes, not the file names. 请注意,生成的类文件以java类命名,而不是文件名。 Look at below examples. 请看下面的例子。

In the below illustrations X is any valid name (except Foo and Bar ). 在下面的插图中, X是任何有效的名称( FooBar除外)。

Example 1: 例1:

// filename X.java (doesn't compile)
public class Foo {

}
public class Bar {

}

compiler complains about public classes Foo and Bar not being present in their own .java files. 编译器抱怨公共类FooBar没有出现在他们自己的.java文件中。

Example 2: 例2:

// filename Foo.java (doesn't compile)
public class Foo {

}
public class Bar {

}

error same as above but applies only to Bar this time. 错误与上述相同,但这次只适用于Bar

Example 3: 例3:

// filename Foo.java (compiles)
public class Foo {

}
class Bar {

}

generated files are Foo.class and Bar.class . 生成的文件是Foo.classBar.class

Example 4: 例4:

// filename X.java (compiles)
class Foo {

}
class Bar {

}

generated files are Foo.class and Bar.class . 生成的文件是Foo.classBar.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM