>> 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.
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 ]
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 ?
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 ]
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 by:autobot
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, the permaculture playing cards