Why is the main method in Java public?
-
What is its use if the class containing the main method is itself default (no access specifier)? How JVM is able to access a main method in a default class? If in any class a method is public and the class containing that method is itself default(i.e. no access specifier) then we know that method can not be accessed by any class outside the package. So how JVM is able to execute main method when we write it into a non public or default class?
-
Answer:
http://docs.oracle.com/javase/specs/jls/se7/html/index.html says nothing about the type of the class which contains the main method. However, main method must be public. (See section 12.1 from the specification.) The main method being static, the JVM can call the method without creating an instance of the class containing it. Hence the class being non-public does not make a difference as far as the entry point of JVM is concerned.
Aniket Garg at Quora Visit the source
Related Q & A:
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- How to get actual return type of a generic static method in Java?Best solution by stackoverflow.com
- Why is the main page of my website not the first search result for my website?Best solution by wordpress.org
- How to Get Main output into Common() method in Java?Best solution by pages.cs.wisc.edu
- Why is it important to have a Public Relations campaign?Best solution by wemagazineforwomen.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.