This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes why to save same name as class 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 » Java » Java in General
Bookmark "why to save same name as class" Watch "why to save same name as class" New topic

why to save same name as class

pradipta kumar rout
Ranch Hand

Joined: Sep 13, 2010
Posts: 43

As we know that to save the java file when the class is public ,we have to give the same name to both class and the fie.
but why this is ?

kindly give me some sugesstion .

Thank you
Steve Luke

Joined: Jan 28, 2003
Posts: 4167

The compiler has to have some way to find the .java files it needs to compile, and to link those .java files with the classes which are stored in them and required elsewhere. The makers of Java decided that the way to provide the link between the .java file and the Class name was to force them to be the same,

Matthew Brown

Joined: Apr 06, 2010
Posts: 4344

Because that's the way the language is designed. It's one of those things that's not worth thinking too much about - the language designers had various decisions to make, and this was what they decided. Why use semi-colons at the end of the line? Because that's how it works.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

In addition to Matthew's post, it have something to do with abstraction. If we as the Java programmer had to worry about things like likes classes and files having the same name and what symbol to use at the end of the line to terminate a command(";" or "." or " ") programming in Java would be A LOT more complicated. It is abstraction that gives high-level languages the ease of programming we know as Java or PHP or Visual Basic programmers.


"If the facts don't fit the theory, get new facts" --Albert Einstein
I agree. Here's the link:
subject: why to save same name as class
Similar Threads
class name
Outer-Inner Question
Interesting Question!!!
why do we need to save the java file with the name of public class file
why public class name and source must be equal