File name matching class name
Title
Question
Is it necessary that class name should match the file name as stated in the tutorial. It seems to work even when the file name does not match the class name.
I think it is required only for public classes.
I could even define multiple classes in a single file also, none of those was public class and so did no find any problem while compiling and got the class files created for each of the class definition in a single java file.
Java First-Java-Program 02-03 min 10-20 sec
Answers:
Dear Pravin,
It is convention to save a file matching the class name though not mandatory when there is no public class defined in the java file.
1. If you have a Public class in the java file, the file name has to match the class name otherwise it will give an error.
2. If the java file does not have any public class declared then it is not mandatory to save the file as the class name. But the java compiler will find out all the classes declared in that file and create .class files with the class names.
For eg: if you define two classes Test1 and Test2 which are not public and save it as XYZ.java . After compilation the java compiler will create two .class file named Test1.class and Test2.class. It is an overhead for the compiler to look for all the class in the file and create a .class file.
So it is a good practice to save the filenames as class names.
Abhijit Bonik
ST Web Team
That is precisely the point.
The tutorial is giving it as a rule that class name should always match the file name.
It needs to be clarified in the tutorial.
In fact a Java file like the following compiles absolutely fine:
A file named Hello.java containing the following definitions.
// Hello.java
class HelloWorld {
public static void main(String[] a) {
System.out.printf("Hello world\\n");
}
}
// javac Hello.java
/**
This is HelloInterface, used as a demonstration to show
that we can have application in interfaces also.
This is possible from Java version 8 and onwards.
*/
interface HelloInterface {
/**
This method prints a greeting to all its command line arguments.
This method is the application method.
@param b - names of friends to be greeted
*/
public static void main(String[] b) {
System.out.println("Hello interface");
System.out.println(b.length);
for (int i = 0; i < b.length; i++) {
System.out.println("Hello "+b[i]);
}
}
}
// java HelloWorld Tom Dick Harry
enum HelloEnum {
;
public static void main(String[] a) {
System.out.println("Hello enum");
}
}
@interface HelloAnnotation {
}
class HelloWorld {
public static void main(String[] a) {
System.out.printf("Hello world\\n");
}
}
// javac Hello.java
/**
This is HelloInterface, used as a demonstration to show
that we can have application in interfaces also.
This is possible from Java version 8 and onwards.
*/
interface HelloInterface {
/**
This method prints a greeting to all its command line arguments.
This method is the application method.
@param b - names of friends to be greeted
*/
public static void main(String[] b) {
System.out.println("Hello interface");
System.out.println(b.length);
for (int i = 0; i < b.length; i++) {
System.out.println("Hello "+b[i]);
}
}
}
// java HelloWorld Tom Dick Harry
enum HelloEnum {
;
public static void main(String[] a) {
System.out.println("Hello enum");
}
}
@interface HelloAnnotation {
}
It is better to use same file and class names
in some two public classes cases error may rises
Login to add comment