This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes name of java file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "name of java file" Watch "name of java file" New topic
Author

name of java file

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Hi All,

Is it necessary that the name of the source file has to be same as that of the class which contains the main() method ?


Regards,<br />Arnab
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

No. There's a rule that every public class must be in a file whose name matches the class, though.


[Jess in Action][AskingGoodQuestions]
Henrique Boreg
Greenhorn

Joined: Aug 11, 2005
Posts: 23
not only the ones which has the main method...it also applies to the ones which do not have it


"I wrote an all-Java OS in 1998 but can't be sure if or how it works... it's still booting"
Henrique Boreg
Greenhorn

Joined: Aug 11, 2005
Posts: 23
Originally posted by Ernest Friedman-Hill:
No. There's a rule that every public class must be in a file whose name matches the class, though.


I didn't know that...my bad
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
ok. so that means every java source file can have at the most one public class. Correct ?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9048
    
  10
That's correct. How much code have you written so far?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> every java source file can have at the most one public class per file?

Incorrect. There is no such requirement. Moreover, there is no requirement that you use files at all. This is a common misconception but as a practical matter, most Java compilers insists on this restriction to speed up compilation when looking for depencencies.

Is there a practical benefit for you? Sure. You, too, can find out the location of a public class without poking around inside binary structures.

http://java.sun.com/docs/books/tutorial/java/interpack/createpkgs.html
[ September 03, 2005: Message edited by: Rick O'Shay ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Rick]: Incorrect. There is no such requirement.

Mmmh. This is a fine point. The JLS specifies that when using files (as most of us do, and as stipulated in the question here) there may be a requirement (on a given system, using a given compiler) that the file may contain no more than one public top-level class. Which means that, if you want to write code that will compile, and you don't know the exact details of the compiler and platform, you do at least know that such a requirement may exist, and so you should write your code to conform to this requirement. Otherwise there should be no surprise when the code does not work. Moreover as a practical matter, Sun's javac compiler has always (as far as I know, since 1.1 at least) enforced this requirement.

For purposes of a discussion in Java in General (beginner), the simple answer is "yes, a file can have no more than one public top-level class".
[ September 03, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
ok. so this is what I understand....

1. main() method must be public
2. class containing the main() method need to be public
3. If there are multiple classes in a single java source file, all of them can contain main() method. There are no restrictions.
4. If there are multiple classes in a single java source file, all of them can contain main() method. And All of these main() methods have to be dclared public.
5. If there are multiple classes in a single java source file, and class One is NOT public but it contains main() method while class Two is public but does not contain main() method. Name of the java source file have to be "Two".
6. If there are multiple classes in a single java source file, and class One is NOT public but it contains main() method while class Two is public and also contains main() method. Name of the java source file have to be "Two".
Are these correct ?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9048
    
  10
ok. so this is what I understand....

1. main() method must be public

right, if you plan to use it to run your program (java MyClass) which is usually what the main() method is used for.

2. class containing the main() method need to be public

wrong, see the answer to your other thread.


3. If there are multiple classes in a single java source file, all of them can contain main() method. There are no restrictions.

right

4. If there are multiple classes in a single java source file, all of them can contain main() method. And All of these main() methods have to be dclared public.

right, sort of (see question #1)

5. If there are multiple classes in a single java source file, and class One is NOT public but it contains main() method while class Two is public but does not contain main() method. Name of the java source file have to be "Two".

right

6. If there are multiple classes in a single java source file, and class One is NOT public but it contains main() method while class Two is public and also contains main() method. Name of the java source file have to be "Two".

right
[ September 03, 2005: Message edited by: Marilyn de Queiroz ]
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Thanks for your reply. Everything looks quite clear now.

For point 2 [from the other threads]

I meant if the program has to be executed, then theoretically class containing the main() method should also be public. However, practically, it has been seen that even without public, it works. Correct ?
[ September 04, 2005: Message edited by: Arnb Sen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: name of java file