Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class name

 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it necessary to give file name same as the class name which has main method.
 
Rushikesh Wagh
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Question. I also Like To Get The Answer Of This Question.
Come On Man I M SCJP 1.4 With 83% Yet I Dont Know This.
to Me.
 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Girish Nagaraj:
Why is it necessary to give file name same as the class name which has main method.



Bcoz we defined the main method as Public static void main(String args[]).and b'coz of static we call the class without cretae any instance of it.that's y we give the file name same as class name which has main method.

I hope i able to clear ur doubt.Awaiting for comments
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to this question is : Think from JVM perspestive. JVM knows the class name only. So it tries to find out main() method in the class having the same name as the .class file.

If you have different Class name from .class file how will compiler knows where to find the main() method by just knowing the .class file


Let me know if you have any question or need more information on this.
 
Narsimha anumolu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Girish,

Class name need not be same as File(program) name. to avoid confusion Both names should be same. we can give both names different but u should be carefull while running the program.

while executing the program u should follow the following procedure.


Compilation ::: javac ProgramName.java
Run :: java ClassName

Here class name is class which is public and contains main() method.

I think now u got it


Regards,
Narsimha Anumolu
HYD
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narsimha,

Will get Runtime Error:java.lang.NoClassDefFoundError

If do it as you said.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file name needs to match the top-level class name only if that class is public. It has nothing to do with a main method.

You can define a top-level class with default access and place it in a file with an entirely different name. As mentioned above, you compile using the file name, then run (assuming a main method) using the class name.

For example...

At the command prompt, use...
javac SomeFile.java
java SomeClass

Note that if SomeClass were public and you tried to compile it as SomeFile.java, then you would get a compilation error telling you that SomeClass "should be declared in a file named SomeClass.java." And if you try to run "java SomeFile," then you will get the runtime error NoClassDefFoundError.
[ May 03, 2006: Message edited by: marc weber ]
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a public class then the file name has to be the name of that public class for the program to compile(that's what most compiler's expect, but its not a language specification.)

But if you don't have a public class in the file then the file name can be different.
javac filename.java
But while running you have to give the name of the class having the main().
java classname (class having main method)
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No here is some confusion over other rancher...

I thought sheriffs will come up with the solution...

nyways I should clear that this was the situation earlier then Jdk1.4 now 1.4 onwards you can create the java file with any name

and the process of compilation and running the class is as follows:




And I can tell you I am not getting any exception on jdk 1.4



 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Girish Nagaraj:
Why is it necessary to give file name same as the class name which has main method.


I disagree to above statement

As per my understanding file name shall be same as public class declared in file.if you donot have any public class in a file.YOu can give any name to your .java file.

YOu can save example below with any name,I saved it as MyTest.java
compile it using javac MyTest.java
and run it with java TestMain



}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic