File name and class name different in java

2019-03-01 01:22发布

问题:

I created a file named In.java an entered the following code

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

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 . now when I run java Init shows an error as Error: Could not find or load main class In Now I again renamed the class as 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

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 . 1. If the class name and file name are different won't the compiler show up an error? 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 ? so can one class file use any other class file in the same directory?

回答1:

Any class which is declared public should be kept in a file of the same name. 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.

Note that the generated class files are named after the java classes, not the file names. Look at below examples.

In the below illustrations X is any valid name (except Foo and Bar).

Example 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.

Example 2:

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

}
public class Bar {

}

error same as above but applies only to Bar this time.

Example 3:

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

}
class Bar {

}

generated files are Foo.class and Bar.class.

Example 4:

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

}
class Bar {

}

generated files are Foo.class and Bar.class.



标签: java class