File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Access modifier for main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifier for main method" Watch "Access modifier for main method" New topic

Access modifier for main method

sangeetha hariharan

Joined: May 28, 2004
Posts: 11

I have one doubt.Is it true that the main method can be marked with an access modifier other than "public"?.can we mark it with some other access modifier?I got this doubt because it is mentioned in the book "scjp exam study guide by katthy sierra and Bert Bates" that 'for the purpose of the exam assume that the main method should be marked public'.
could anyone please explain it???
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
Well, Yes the main method you use to start an application must be public! Now having said that, there is a bug with some releases of the JDK which will allow an app to start even if the main method is declared private. Also, this is only if you wish to start the app with that, nothing prevents you from declaring the main method with another access modifier and calling it from an already running app under the constraints of the access modifier chosen. The main method is just another method. To use it to start an application the signiture MUST be:
public static void main(String[] carl)

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Jeff Langr
Ranch Hand

Joined: May 14, 2003
Posts: 799
Originally posted by Carl Trusiak:
To use it to start an application the signiture MUST be:
public static void main(String[] carl)

Drat, no wonder I was having problems, I was using:

all along.


Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
I agree. Here's the link:
subject: Access modifier for main method
It's not a secret anymore!