aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes public class or not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "public class or not" Watch "public class or not" New topic
Author

public class or not

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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
Please show us your code, and rephrase your question a little.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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
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
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
try this code:

Even though class TestMain is not public, it still works.
Yan


Ian
Whiz Kid
Greenhorn

Joined: Jul 02, 2002
Posts: 6
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
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public class or not
 
Similar Threads
Two public classes in a file
Query -> inheritance
Code problem or logic problem ?
Why one public class
URGENT