aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Source File declaration rule in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Source File declaration rule in java" Watch "Source File declaration rule in java" New topic
Author

Source File declaration rule in java

Vidya Laxman
Greenhorn

Joined: Jul 01, 2009
Posts: 3
Why is it that the source file name must match with public class name ?
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

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

And welcome to java ranch.....


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

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 - More SCJP Mocks
Vidya Laxman
Greenhorn

Joined: Jul 01, 2009
Posts: 3
Please Explain...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Vidya Vidya wrote:Please Explain...

What??
What do you want to know exactly ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Vidya Vidya wrote:

Please check your private messages for an important administrative matter.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Vidya Laxman
Greenhorn

Joined: Jul 01, 2009
Posts: 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
Posts: 813

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


Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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>
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1000
    
    1

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


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Source File declaration rule in java
 
Similar Threads
Source File
Empty file
source file?
A very simple question (Please Answer)
Java file is compiling but not running.