Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Source File declaration rule in java

 
Vidya Laxman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it that the source file name must match with public class name ?
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it is the file where the JVM will look for public static void main(String a[]) method....

And welcome to java ranch.....
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidya Laxman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Explain...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya Vidya wrote:Please Explain...

What??
What do you want to know exactly ?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya Vidya wrote:

Please check your private messages for an important administrative matter.
 
Vidya Laxman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic