Forums Register Login

name of java file

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
No. There's a rule that every public class must be in a file whose name matches the class, though.
+Pie Number of slices to send: Send
not only the ones which has the main method...it also applies to the ones which do not have it
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
ok. so that means every java source file can have at the most one public class. Correct ?
+Pie Number of slices to send: Send
That's correct. How much code have you written so far?
+Pie Number of slices to send: Send
>> 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 ]
+Pie Number of slices to send: Send
[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 ]
+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
Hang a left on main. Then read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1355 times.
Similar Threads
please help me out.. exam tomorrow
hey guys
how to make a war file
FTP
How to I know the my URLyBird version?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:08:33.