What if we put private/protected access specifier to a main method in Java Class?
//Code goes here
When JVM couldn't find the exact syntax it throws the Error with suggestive syntax as given above.
Why JVM search for public access specifier only in the syntax?
It's because JVM starts execution with main method and it can access public access specifier easily, as public specifiers can easily be accessed from outside the class.
//Code goes here
package indoscopy;
public class PrivateMainMeth {
private static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("I'm inside a public main method");
}
}
Answer: The program will compile successfully, but will throw a run-time error
Error: Main method not found in class indoscopy.PrivateMainMeth, please define the main method as:
public static void main(String[] args)
Why we get this "Main method not found ..." Error?
We get this error because during any core Java program execution, JVM starts with main method and search for this particular syntax
public static void main(String[] args)
When JVM couldn't find the exact syntax it throws the Error with suggestive syntax as given above.
Why JVM search for public access specifier only in the syntax?
It's because JVM starts execution with main method and it can access public access specifier easily, as public specifiers can easily be accessed from outside the class.
No comments:
Post a Comment