aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Naming a java file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Naming a java file?" Watch "Naming a java file?" New topic
Author

Naming a java file?

srikesh krishnan kutty
Greenhorn

Joined: Dec 12, 2010
Posts: 5
Why is it compulsory that the name of the java file must match the name of the public class ??
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
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

Joined: Dec 07, 2010
Posts: 41
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Naming a java file?
 
Similar Threads
Uploader?
ClassNotFoundException error
please help me out.. exam tomorrow
KB 791
Interface