• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query

 
ajay_kumar_rana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have n number of non public classes in a file that has a different name(not any of the class names).
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunny,

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


instead of :

 
Puneet Mittal
Ranch Hand
Posts: 45
Eclipse IDE Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic