GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Why do we need to give the same file name as the class name in a public class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do we need to give the same file name as the class name in a public class?" Watch "Why do we need to give the same file name as the class name in a public class?" New topic
Author

Why do we need to give the same file name as the class name in a public class?

Saumya Purkayastha
Greenhorn

Joined: Mar 27, 2010
Posts: 23
I was trying to run a program - phraseOMatic.java which was not working, when I changed the filename to PhraseOMatic.java(which is same as the public class name), it started working.

I got the below error:-

1 error

C:\Program Files\Java\jdk1.6.0_18\bin>javac C:\Users\Sony\Desktop\java\phraseOMatic.java
C:\Users\Sony\Desktop\java\phraseOMatic.java:1: class PhraseOMatic is public, should be declared in a file named PhraseOMatic.java
public class PhraseOMatic {
^

Why is it that in a public class we have to name the .java file exactly as the class name.

I was able to compile this program after I changed the file name from phrasOMatic.java to PhraseOMatic.java

I have noticed that if we do not specify the class as public, the file name can be totally different from the class name.

Thanks and Regards, Saumya Purkayastha - Sam
Specialist - Internet Marketing
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Saumya Purkayastha wrote:
Why is it that in a public class we have to name the .java file exactly as the class name.

Because the Java Specifications says that - see here:
http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.6 Top level Type declaration unit


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.
Saumya Purkayastha
Greenhorn

Joined: Mar 27, 2010
Posts: 23
Is this restriction only for Public class?

If so why is this restriction not there for non-public classes in Java specification?

Sorry if I am too inquisitive. Beginners often want to know "why?"
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
Is this restriction only for Public class?

That's quite easy to test, no?


Ping & DNS - my free Android networking tools app
Kenny Kuchera
Greenhorn

Joined: Mar 16, 2010
Posts: 9
Ulf Dittmer wrote:
Is this restriction only for Public class?

That's quite easy to test, no?


True, but I do think its the same for non-public classes.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

Kenny Kuchera wrote:True, but I do think its the same for non-public classes.

No it isn't, as people have already said above. Try it out yourself!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Saumya Purkayastha
Greenhorn

Joined: Mar 27, 2010
Posts: 23
If you know the answer please let me know rather than asking me to test. This is a request because it will be helpful for the community other than me. I have tested as per your recommendations and found the following results:-

I created a simple program and tried to use different access specifiers for the class definition. Below are the results:-

Public - In this case the class name has to be same as the filename.
Not specified anything - The filename and class name need not match.
Protected - It does not allow me to use this.
Private - It does not allow me to use this.

This is my 4th program and so I dont know the reason to why this is happening. I am sure I will find this either in this forum soon or in my future days of studying java.

I have great expectations from this forum. Gimme a satisfactory answer.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Saumya Purkayastha wrote:If you know the answer please let me know rather than asking me to test.

Why? If you test it yourself, you learn better, and can then follow up with the results.
This is a request because it will be helpful for the community other than me. I have tested as per your recommendations

See? Now you've actively participated in the community, contributed back, learned how to learn, and have become that much more self-sufficient.
Gimme a satisfactory answer.

You were actually already told the answer: because this is how Java is specified.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Private and protected access for classes are only available when those classes are inside other classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do we need to give the same file name as the class name in a public class?