Forums Register Login

Naming a java file?

+Pie Number of slices to send: Send
Why is it compulsory that the name of the java file must match the name of the public class ??
+Pie Number of slices to send: Send
Because the designers said so.

Personally I think it's great, because I don't have to look for hours to find the implementation code of something.
+Pie Number of slices to send: Send
Naming a java file same as the public class is due to the running phase of the java file or a java class

----The steps to run a java file is
1.first the java compiler is used to compile the java file to provide an output .class file.{COMPILER phase}
2.this output .class file is interpreted by JVM{Interpreter phase}

For example to run file which has class with main method:


[javadoc]public class ABC{
public static void main(String[] args){
System.out.println("hello");
}
}

//other supporting classes
class A{
}[/javadoc]



Example 1:
File aABC.java has a public class ABC
Now during compilation of ABC.java will produce a byte code .class file ABC.class
We need to run this ABC.class file to run our program.

command prompt execution:
>javac ABC.java
>java ABC

Example 2:
File XYZ.java has a public class ABC
Now during compilation of XYZ.java will produce a byte code .class file ABC.class
We need to run this ABC.class file to run our program

command prompt execution:
>javac XYZ.java
>java ABC


---------------------------------------------------------------------------------
This process leads to confusion in case of some large application.
Some times even these small programs also confuse due various process to run the file.

To reduce the confusion to the user Sun decided to directly make a standard that the public class needs to be the file name so that we can refer to the required class easily.
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 911 times.
Similar Threads
ClassNotFoundException error
please help me out.. exam tomorrow
Uploader?
Interface
KB 791
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:49:36.