File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes class has to be public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class has to be public" Watch "class has to be public" New topic
Author

class has to be public

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Hi All,

Is it necessary that the class in which the main() is defined have to be public ?


Regards,<br />Arnab
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

This is a somewhat grey area. Technically, I believe the answer is "yes". Practically, it's "no" -- most Java implementations will run your class whether its public or not.


[Jess in Action][AskingGoodQuestions]
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
so if we have this question during certification exam, what option are we supposed to select ?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
They won't ask you that question.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[EFH]: Technically, I believe the answer is "yes".

I don't think it is. It's required that the main() method be public (which was originally not enforced by Sun's JVMs, but has been enforced since 1.4 I believe). However I don't see any requrement that the class be public. In fact the JLS has several examples in which the class is not public, but rather package. So I would say that the JVM is required to allow this. And testing confirms this is what JDK 5 does, at least.

So...

[Arnb]: Is it necessary that the class in which the main() is defined have to be public ?

No.
[ September 03, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> Is it necessary that the class in which the main() is defined have to be public?

Yes, it is required. JVMs prior to 1.4 had a bug that allowed non-public main methods to be run. The reaction was appropriate: massive yawn, will not fix.

http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.1.4

This will fail with "main method not public".

class foo { static void main(String ... args) { } }
[ September 03, 2005: Message edited by: Rick O'Shay ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes Rick - as I noted. But the question is about whether the class needs to be public. Not the method.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> But the question is about whether the class needs to be public. Not the method.

I know, I was just testing you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class has to be public