aspose file tools*
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
Author

Access modifier for main method

sangeetha hariharan
Greenhorn

Joined: May 28, 2004
Posts: 11
Hi,

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
Sheriff

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
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
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.

-J-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Access modifier for main method