-1

In java main method if changing access modifier,we got a runtime exception.So,My doubt is,Which class inherted from Java main method?.what are rules to be followed by main() method?.If not inherted,what are internal done by jvm when i calling main method?.

techsuri
  • 431
  • 3
  • 5
  • 10

5 Answers5

3

According to the Java Specification (Third edition) http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.1.4. The method main must be declared public, static, and void. It must accept a single argument that is an array of strings. This method can be declared as either

public static void main(String[] args)

or

public static void main(String... args)

So:

  • public. Modifier of access for make accessible the method
  • static. Modifier that specify there is no necessary create a new object for call the method.
Ernesto Campohermoso
  • 7,213
  • 1
  • 40
  • 51
0
  1. main method should be static since we need to access it without creating an instance.
  2. main method should be public so that it is visible outside the class scope.

Therefore, if you change the access modifier, JVM cannot able to access the main method. It is not inherited.

Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
0

Public - main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application or to indicate that the method can be called by any object.

static - When JVM makes the call to the main method there is no object existing for the class being called therefore it has to have static method to allow invocation from class.

RanRag
  • 48,359
  • 38
  • 114
  • 167
0

It is perfectly legal syntax to change the modifier of a main method to private. The JVM does not require a access modifier of any sort for methods NAMED main. BUT it DOES REQUIRE that classes which are EXECUTED have a public main method.

Im not sure what your exact purpose is, but here are two suggestions.

1) Are you trying to make your code less dependent on "static" invocations by privatizing the main method ? If so, remove the main alltogether, and have your class implement a "runnable" object , rather than supporting a main method. This way, other classes can run your class without relying on it main method.

2) Were you attempting to convert the main method to another, internally used static method of the class ? If so, you might want to reconsider why you are naming such a method the as main (and more significantly, why you are still trying to run this class as a java application entry point if you KNOW its main is private ? )....

jayunit100
  • 17,388
  • 22
  • 92
  • 167
0

According to java docs,The main() method signature must be like this and why i m saying that :

public static void main(String[] args){
//your stuffs
}
  1. public access specifier is used here because JVM is starting executing the java program by calling main() method.So JVM is nothing but a program.So if main() will not be public then JVM cannot call this method.So it must be public
  2. As during the time JVM called main() no object is present because execution starts from main() method onlt.So without object we can only call a method by making it static.So for JVM to call main(),main() must be static,so it is static.
  3. To supply any data inside your program at run time, an aray of String is given because String can be converted to any other datatype easily.
Android Killer
  • 18,174
  • 13
  • 67
  • 90