Forums Register Login

Source File declaration rule in java

+Pie Number of slices to send: Send
Why is it that the source file name must match with public class name ?
+Pie Number of slices to send: Send
Because it is the file where the JVM will look for public static void main(String a[]) method....

And welcome to java ranch.....
+Pie Number of slices to send: Send
 

Vidya Vidya wrote:Why is it that the source file name must match with public class name ?



Thats part of the JVM spec. It allows the JVM to look for the right class name.
+Pie Number of slices to send: Send
Please Explain...
+Pie Number of slices to send: Send
There is no real reason for it, and as far as I know it is not a requirement that is in the JVM specification or the Java language specification. It's just the way that Sun's implementation of Java chooses to organize things.

Once I read somewhere that originally it was implemented this way in the Oak language, which is the predecessor to Java. By requiring that the filename of the source file would be the same as the public class in the source file, some kind of optimization could be done in the compiler for Oak. Java inherited this feature from Oak.
+Pie Number of slices to send: Send
 

Vidya Vidya wrote:Please Explain...


What??
What do you want to know exactly ?
+Pie Number of slices to send: Send
 

Vidya Vidya wrote:


Please check your private messages for an important administrative matter.
+Pie Number of slices to send: Send
 

sudipto shekhar wrote:

Vidya Vidya wrote:Please Explain...


What??
What do you want to know exactly ?



The JVM will recognize the main method even if we dont make the class is public...

I think there has to be a reason for making the sourcefile name same as public class name

So....seeking a bit more clarification in this regard
+Pie Number of slices to send: Send
This restriction, of having the file name same as the public class name, 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 code.wiz.Bank would be found in a file Bank.java in the directory code/wiz, and the corresponding object code would be found in the file Bank.class in the same directory.

Also you should know that it is not mandatory to say "file name equals to classname". You 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 your program into JVM you just have to give the class name. This is possible even the main() is public/private.


[edit] We say this statement that the file name should be same as the class name to make sure there is no confusion while compiling and running the program. Consider you have created many programs in java and now you want to run any one of them ,then it would be very difficult for you to recall the class name of that particular program. So to make it a simpler we often say that the class name should be same as the file name. [edit]

Hope this makes things better


+Pie Number of slices to send: Send
If a file doesn't contain a class with a public accessor and if it contains one or more classes with the default accessor (package level access), then the filename of the file can be anything that you choose. This is true even if the file contains a class that has the public static void main(String [] args) method. For example, the class below is the sole class in the file StringTest.java



C:\TEMP>javac StringTest.java

C:\TEMP>java StringTest
10String7

C:\TEMP>
+Pie Number of slices to send: Send
Hi Harry

Does your below mentioned line got compiled correctly?
C:\TEMP>java StringTest

I tried it at my end and it gave error

Thanks
Abhay

+Pie Number of slices to send: Send
Hi Abhay,

The Java ..\bin directory has to be in your PATH environment variable on a WINDOWS machine. The fully qualified PATH to the ..\bin directory is dependent on where in your harddrive directory hierarchy you installed Java.

Using the code exactly as listed in this thread, I was able to invoke the following commands:

C:\TEMP> javac StringTest.java

This command compiles the code.

C:\TEMP> java StringTest

This command invokes the compiled bytecode.

This example will not work if your PATH environment variable in WINDOWS is not configured correctly. REMEMBER: you have to reboot your computer after you configure the PATH environment variable. The new PATH doesn't become effective until your computer has been rebooted.

The CLASSPATH variable has to be defined in your environment variables. CLASSPATH should be equal to .; this is a dot followed by a semicolon.
reboot your computer after creating the CLASSPATH and modifying the PATH.

Regards, Harry
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1928 times.
Similar Threads
source file?
Source File
A very simple question (Please Answer)
Empty file
Java file is compiling but not running.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:31:16.