Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

name of java file

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. There's a rule that every public class must be in a file whose name matches the class, though.
 
Henrique Boreg
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not only the ones which has the main method...it also applies to the ones which do not have it
 
Henrique Boreg
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. so that means every java source file can have at the most one public class. Correct ?
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct. How much code have you written so far?
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> 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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic