Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes class name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class name" Watch "class name" New topic

class name

Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Why is it necessary to give file name same as the class name which has main method.
Rushikesh Wagh
Ranch Hand

Joined: Jun 18, 2005
Posts: 58
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.

Best Regards,
Rushikesh Wagh
SCJP 1.4 (2005)
SCJP 1.6 (2010)
Nidhi Jain
Ranch Hand

Joined: Mar 26, 2006
Posts: 31
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

Joined: Feb 27, 2006
Posts: 390
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

Joined: May 03, 2006
Posts: 4
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
Run :: java ClassName

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

I think now u got it

Narsimha Anumolu
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Hi Narsimha,

Will get Runtime Error:java.lang.NoClassDefFoundError

If do it as you said.
marc weber

Joined: Aug 31, 2004
Posts: 11343

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...
java SomeClass

Note that if SomeClass were public and you tried to compile it as, then you would get a compilation error telling you that SomeClass "should be declared in a file named" And if you try to run "java SomeFile," then you will get the runtime error NoClassDefFoundError.
[ May 03, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
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.
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

Joined: Dec 27, 2005
Posts: 1249

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

Joined: Dec 21, 2005
Posts: 70
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
compile it using javac
and run it with java TestMain

I agree. Here's the link:
subject: class name
Similar Threads
Struts iteration
3rd party library problem
Making Connection with database
About Named Queries
Need help with set and get using a scanner input with three different classes