- can the main () method be specified as private or protected?
Yes
will it compile?
Yes
will it work
Yes, but this cannot be made an application entry point. It will work if it is called from another place.
Try:
$cat PrivateMain.java package test; public class PrivateMain { protected static void main( String [] args ) { System.out.println( "Hello, I'm proctected and I'm running"); } } class PublicMain { public static void main( String [] args ) { PrivateMain.main( args ); } } $javac -d . PrivateMain.java $java test.PrivateMain Main method not public. $java test.PublicMain Hello, I'm proctected and I'm running
In this code, the protected method cannot be used as the entry point of the application, but it can be called from the PublicMain class
Private methods cannot be called, but from the class itself. So you will need something like:
public static void callMain() { main( new String[]{} ); }
To call main if it was private.
source share