answersLogoWhite

0


Best Answer

First of all, it only has to be the same when the class is public. And there is no explicit reason for that, it's just a convention that came along with old versions of java and people got used to it... They say it's because of the limited capabilities of the compiler to compile dependencies. When packages are stored in a file system (?7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true: * The type is referred to by code in other compilation units of the package in which the type is declared. * The type is declared public (and therefore is potentially accessible from code in other packages). This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory. When packages are stored in a database (?7.2.2), the host system must not impose such restrictions. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units. It is not mandatory to say "file name equals to classname". > U can give your own name to your filename [ other than classname ] > at the time of compilation you just give your filename[other than classname] > After compilation you will get .class file with your class name.[classname.class] >.But at the time of loading ur program into JVM u just have to give the class name , This is possible even the main() is public/private. for eg:-consider have created a program in java with file name Ashish n class name is batra,now at the time of compilation u have to write "javac ashish.java" at the command prompt and at the same time the jvm create the .class object in the bin directory with filename =batra(batra.class) .Now at the time of running the program u have to write "java batra" at the command prompt. We say this statment that the file name should be same as the class name to make sure there is no confusion while compiling n running the program .Consider u have created many programs in java and now u want to run any one of them ,then it would be very difficult for u to recall the class name of that particular program .So to make it a simpler we offenly say that the class name should be same as the file name.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

The Java class name and the Java file name have a one to one relationship. The Java file name should match the name of the top most public class in the file. If this is not followed, the compiler would not let you compile the file.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

First of all, this only applies to the top-level class. Within your class, you can declare any number of local inner classes.

As far as I can tell, this is mostly done to remove useless confusion over file names. Why even give a programmer the option to give poor names to their source files when you can force them to use a good naming scheme?

This also simplifies the job of the Java compiler, as it knows that when it's looking for a class with a given name it can just look in the file with the same name.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Because that is how Java file naming conventions are designed. The rules for naming files in Java are as follows:

• There can be only one public class per source file.

• Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here.

• If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Rock { } must be in a source code file named Rock.java.

• If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.

• If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.

• import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.

• A file can have more than one nonpublic class.

• Files with no public classes can have a name that does not match any of the classes in the file.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Because to provide consistency, next if you don't do that you can get errors such as FileNameNotFoundException.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

It's not exactly that way. You can put all the class files in a project into a single file, so the file name tells what the main class in the file is.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do file name and public class name always coincide in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you call main class with in main class in java?

We can't call a class. We always call a method in java.


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


The name of a Java program file must match the name of the class with the extension java?

not exactly..... only If your class is public then the java program name should be the public class name with extension Sample.java >> public class Sample { public static void main(String[] args) { ..... } } NonPublicClass.java class SomeOtherName { ......... }


How a class in java declared default?

default it is public type


Why the name of a java file is same as the name of the public class?

It is not compulsory that the java file name and name of the public class should be same. if u will give java file name and public class name different then u have to compile and run the program with another names. for example: u have named class sample i.e. (public class sample) and main function is also defined in this class. and u have saved the file as abc.java then u will first comple the file as: javac abc.java now run the file (type java class name(in which main function is defined) i.e. java sample try it.


Is it possible to have more than one public class in the same file?

No. There can be multiple java classes in the same .java file, but the name of the file must match the name of the public class in the file.


Java program file must match the name of class with extension true or false?

The name of the .java file should exactly match with the name of the public class in the file. Ex: public class Test { ..... } this file should be saved as Test.java


What are the two ways of spawning a thread in Java?

You can create a Thread in Java by using two ways. 1. Extending the Thread class public class Test extends Thread { ..... } 2. Implementing the Runnable Interface public class Test implements Runnable { ... }


Can a .java file contain more than one java classes?

Yes, it can. However, there can only be one public class per .java file, as public classes must have the same name as the source file.


What does public mean in java?

The keyword public is an access specifier. A variable or a method that is declared public is publicly accessible to any member of the project. Any class or method can freely access other public methods and variables of another class.


Why would you make a class final in java?

You would make a class Final in Java if you do not want anybody to inherit the features of your class. If you declare a class as Final, then no other class can extend this class. Example: public final class X { .... } public class Y extends X { .... } Here Y cannot extend X because X is final and this code would not work.


Extension file for Java?

Java source files have the .java extension, compiled Java class files have the .class extension.