2

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?

Aubin
  • 14,617
  • 9
  • 61
  • 84
xtreak
  • 1,376
  • 18
  • 42
  • 2
    Could you add some indentation to your code? This is very hard to read. You can use `ctrl + shift + f` in eclipse to automatically format it. – Jeroen Vannevel Nov 01 '13 at 17:20
  • Sorry that I use a text editor for java @JeroenVannevel – xtreak Nov 01 '13 at 17:21
  • you might want to consider to start with an actual IDE like Eclipse or Netbeans. It will prevent a lot of mistakes and it makes developing a bazillion times more comfortable. – Jeroen Vannevel Nov 01 '13 at 17:24
  • Ya but some books recommend me to start with text editor and command line to understand better. – xtreak Nov 01 '13 at 17:27
  • Yes you're right. A console (nash or cmd.exe), vi (or notepad), and a compiler (gcc or javac) is the minimum you need. IDE is the next step. – Aubin Nov 01 '13 at 17:30
  • Well its a compiler restriction it seems refer to http://stackoverflow.com/questions/1958456/why-cant-i-give-different-name-to-class-than-the-file-name . But I cant understand the pascal post @Aubin – xtreak Nov 01 '13 at 17:52

1 Answers1

5

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.

  • Well if I dint add the public access specifier it compiles. Here the class name is Inheritance and file name is In.java . It compiles to give Inheritance.class – xtreak Nov 01 '13 at 17:25
  • Ya . But it doesn't act according to the language specification then it shouldn't compile right? – xtreak Nov 01 '13 at 17:33
  • Well its a compiler restriction it seems refer to http://stackoverflow.com/questions/1958456/why-cant-i-give-different-name-to-class-than-the-file-name . But I cant understand the pascal post @Nishant . If you know explain me too. – xtreak Nov 01 '13 at 17:51
  • @xtreak Could you elaborate? I did't really understand your concern. –  Nov 01 '13 at 18:07
  • I couldn't explain the post by Pascal in the link that I posted in the comment. It seems relevant to the question. – xtreak Nov 01 '13 at 18:14