This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "query" Watch "query" New topic
Author

query

ajay_kumar_rana
Greenhorn

Joined: Feb 03, 2008
Posts: 6
Need to know is it necessary that the source file name matches the name of one of the java classes present in it.?
Lave Kulshreshtha
Ranch Hand

Joined: Oct 21, 2007
Posts: 106
Name of java file should be same as the public class present inside it.

-Lave

Originally posted by AjayKumarRana:
Need to know is it necessary that the source file name matches the name of one of the java classes present in it.?


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, ITIL V3 Foundation Certification
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Actually this is not necessary..!!!
but it is a good programing practice if you do so..!! I tell how it works:

Suppose you have one java file name : Hi.java
inside this you have following code :



If you compile this code, this will compile fine..But after compilation..it generate a class file..The name of class file is decided by compiler..and compiler pick the same name as that of parent class in the java file ..so after compilation the class file generated will be : Hello.class

So in order to avoid confusion..it is always a good practice to keep source file name as that of parent class name.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19678
    
  18

As Lave said,
Name of java file should be same as the public class present inside it.

I highlighted the word "public", since you CAN have a different name as long as all classes have default access (i.e. no public, protected or private). As soon as there is one public class (and there can be only one per file), the file name MUST be the same as that class name.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
You can have n number of non public classes in a file that has a different name(not any of the class names).


Cheers!
RSR
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Hi Sunny,

Your explanation seems wrong atleast for java 1.5.
You should have written:


instead of :

Puneet Mittal
Ranch Hand

Joined: Dec 18, 2007
Posts: 45

i agree with the above point that we can define n number of non public classes in a file.but what if i define some around three non public classes and all the three classes containing main method and each class givin cal to other class's main method.
check this link and explain me please.

http://www.coderanch.com/t/411098/java/java/why-code-runs-successfully
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: query