Consider the following code, where the HelloWorld
class has default or package-private access:
class HelloWorld {
public static void main(St
You have not made it very clear, but I assume that your question is why that main method can be run when you type java HelloWorld
at the command line.
The answer is that the Java Language Specification simply does not require the class that contains the main method to be public. Access modifiers are a language mechanism mainly intended to help maintainability via encapsulation. They're not really a security feature, and certainly not unshakable laws of physics. The JVM startup mechanism simply ignores them.
In fact, you can even use a private inner class, and it will still run.