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 In
it 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?