Hi Guys: The class in a file that has the main() method: Is it required to be public and only public in that file? I thought so all along the way but then I saw code that had no public class and it still runs.... Thanks Barkat
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
Please show us your code, and rephrase your question a little. -Barry
The main() method must be public (and static and void), but the class itself need not be.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barkat Mardhani
Ranch Hand
Joined: Aug 05, 2002
Posts: 787
posted
0
Hi Ron: So if class with main() is not public, it is not accessable from outside of package: True?
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
That's correct. If the class is public then it must be the only one that's public in the file. And the filename must be the same as the public classes name (with .java of course) -Barry
Ian Wayne
Greenhorn
Joined: Aug 03, 2002
Posts: 8
posted
0
try this code:
Even though class TestMain is not public, it still works. Yan
Ian
Whiz Kid
Greenhorn
Joined: Jul 02, 2002
Posts: 6
posted
0
In each Source File there can be one and Only One public class which hosts the main method.Thats the only way the runtime System and statrt working on your program. There is no way a program w/o a public class can execute.is so can u please paste that code here. Hope this helps. Gowri
gowri Venkatesh
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
A source file can have any number of top-level classes, but at most one of them can be public. If there is a public class, its name must match the name of the file. Any class, public or not, can have a method with the signature public static void main (String[] args) and if you feed such a class to the java interpreter, that method will execute.
Ian Wayne
Greenhorn
Joined: Aug 03, 2002
Posts: 8
posted
0
There is no way a program w/o a public class can execute.
Hi Whiz, Question is why the code I posted before works fine. Thanks.