answersLogoWhite

0

Why you same name in class and file in java?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

For run the program

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you same name in class and file in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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.


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.


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.


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 do you make a java class?

You write the source code, in a text editor, or better in a special IDE. The source code should have the extension ".java". You can have several classes in the same file. Then you compile the class to bytecode; this creates a file with extension ".class".


How do you import a self created class in java?

It's the same as importing any other class: import [package name].[class name];


Java runtime error - NoClassDefFoundError -- how to remove this error in java programming?

This error means that there was a call to a nonexistent class, but this was not caught by the compiler since the class existed when the file was compiled but was removed before running the .class file. To fix this problem try restoring any files you deleted to the SAME directory as your .class file. This error can also be fixed by changing the Java classpath to point to the locations of your files. The -cp command line option should help.


Why do file name and public class name always coincide in Java?

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.


The methods cannot have the same name in java true or false?

False. Methods in a class can have the same name as long as they have a different signature. You cannot duplicate method code inside a class but you can always have methods that have the same name but a different signature. Ex: Here I have created two methods in a class that have the same name "sum" but have a different argument types, and return types and hence perfectly allowable in a java class. Public class PolymorphismExample { public int sum(int a, int b){ return a + b; } public double sum (double a, double b){ return a + b; } }


Command use on how to run a java program?

You execute it the same way you would on any other OS. As long as you have the Java Runtime Environment installed and the "java" executable is in your path, from the command line you would simply run: java -cp /path/to/file/here com.some.class.to.run.Here


How do you compile and run with the same file name provided file name and class name are different?

You are supposed to use the same name for the filename and the class name. I am not entirely sure what happens otherwise; at the very least, I would expect that you would get unnecessary complications.


How is it possible that the same Java program can run on different platforms?

Java source code is compiled into .class files, which are used by the Java Virtual Machine (JVM). The format of the .class file is the same for all platforms, and so the source code can be compiled the same way on each platform. The JVM, however, needs to be written to run on a specific platform. This is the part which converts Java bytecode to native bytecode, and is why you need to download the JRE (which contains the JVM) for a specific platform. Java source code is compiled into .class files, which are used by the Java Virtual Machine (JVM). The format of the .class file is the same for all platforms, and so the source code can be compiled the same way on each platform. The JVM, however, needs to be written to run on a specific platform. This is the part which converts Java bytecode to native bytecode, and is why you need to download the JRE (which contains the JVM) for a specific platform.