The most intelligent Java IDE
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of JBoss AS 5 Development this week in the JBoss forum
or Spring Dynamic Modules in Action in the Spring forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

Source File declaration rule in java

Vidya Laxman
Greenhorn

Joined: Jul 01, 2009
Messages: 3

Why is it that the source file name must match with public class name ?
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Messages: 590

Because it is the file where the JVM will look for public static void main(String a[]) method....

And welcome to java ranch.....

Regards,Sudipto
SCJP 5 ScjpFAQ JLS
Deepak Bala
Rancher

Joined: Feb 24, 2006
Messages: 4679

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.

SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
Vidya Laxman
Greenhorn

Joined: Jul 01, 2009
Messages: 3

Please Explain...
Jesper Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7631

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.

Java Beginners FAQ - JavaRanch SCJP FAQ
The Java Tutorial - Java SE 6.0 API documentation
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Messages: 590

Vidya Vidya wrote:Please Explain...

What??
What do you want to know exactly ?

Regards,Sudipto
SCJP 5 ScjpFAQ JLS
Maneesh Godbole
Bartender

Joined: Jul 26, 2007
Messages: 3876

Vidya Vidya wrote:

Please check your private messages for an important administrative matter.

work is the scourge of the drinking class
Vidya Laxman
Greenhorn

Joined: Jul 01, 2009
Messages: 3

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
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Messages: 590

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


This message was edited 1 time. Last update was at by sudipto shekhar


Regards,Sudipto
SCJP 5 ScjpFAQ JLS
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Messages: 143

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>

This message was edited 2 times. Last update was at by Harry Henriques


SCJP-5 88%
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Messages: 260

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


Hunting SCBCD in Java forest created by Sun
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Messages: 143

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

This message was edited 3 times. Last update was at by Harry Henriques


SCJP-5 88%
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
replay challenge