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?

6
  • 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. Commented Nov 1, 2013 at 17:20
  • Sorry that I use a text editor for java @JeroenVannevel Commented Nov 1, 2013 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. Commented Nov 1, 2013 at 17:24
  • Ya but some books recommend me to start with text editor and command line to understand better. Commented Nov 1, 2013 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. Commented Nov 1, 2013 at 17:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
Ya . But it doesn't act according to the language specification then it shouldn't compile right?
Well its a compiler restriction it seems refer to stackoverflow.com/questions/1958456/… . But I cant understand the pascal post @Nishant . If you know explain me too.
@xtreak Could you elaborate? I did't really understand your concern.
I couldn't explain the post by Pascal in the link that I posted in the comment. It seems relevant to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.