• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Naming a java file?

 
srikesh krishnan kutty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it compulsory that the name of the java file must match the name of the public class ??
 
Stephan van Hulst
Bartender
Posts: 6318
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
anirudh jagithyala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic