Why should the class containing the main method not be publicly available in Java?

I wrote the following code

class Hello //Note the class is not public { public static void main(String args[]) { 
System.out.println("Hello"); } }
code>

So, when I run it, it works fine and prints the output of "Hello".

However, if the JVM specification indicates that the main method should be publicly available, since "it cannot see the main thing otherwise," should it not be applied to the class as well? If the JVM does not "see" Hello.main () when it is not declared public, as it can see class A.

Is there any explanation for this other than "because the spec says so"?

And if the JVM can see all classes and methods, since this is the "security / visibility element" itself, then why should the main method be declared as public.

+4
java
Sep 04 '13 at 15:30
source share
8 answers

Just for hits, a demonstration that private classes can also contain main :

 class Outer { private static class Inner { public static void main(String[] args) { System.out.println("Hello from Inner!"); } } } 

Compiles and works fine from the command line:

C: \ junk> javac Outer.java
C: \ junk> java Outer $ Inner
Hello from Inner!

C: \ junk>

+7
Sep 04 '13 at 16:23
source share

if the JVM specification provides that the main method should be publicly available, because "it cannot see the main otherwise"

He can see, but he does not see it as an entry point, and therefore he gives NoSuchMethodError: main if you are trying to execute a class that does not have such a method.

In classic design, the main entry point is

  • Must be called main
  • Must be publicly available
  • Must be static
  • Must be void
  • Must have one argument that contains an array of strings

Hence,

 public static void main(String args[]) 

Being static, the JVM can call it without creating any instance of the class that contains the main method. Not sure if this is the main reason that the main object is static in design.

A class with default access, similar to Hello in your example, is only available for other classes in the same package.

+5
Sep 04 '13 at 15:55
source share

I do not think that the specification states that the class should be publicly available. See the official java tutorial examples. None of the classes with the main method in the examples are declared as Public.

This was discussed earlier in stackoverflow. See Package-private class in .java file - why is it available? Well explains this.

+1
Sep 04 '13 at 15:39
source share

Remember that main is an early language function. I suppose it was thought that a private method might disappear in a .class file, possibly nested, possibly with a shorter name. So this is a simple overriding (?) Convention for finding the right method:

 static void main(String[]) 
0
Sep 04 '13 at 15:39
source share

the default access specifier is package.Classes can access members of other classes in the same package. But outside the package, it looks like a private one, but the JVM has access to all classes, so the JVM can only change visibility in order to find the beginning of the program, so it is the default by default

0
Sep 04 '13 at 15:42
source share

So first, consider this 1. Since the main method is a static Java virtual machine, it can invoke it without creating any instance of the class that contains the main method.

this is: 2. Everything that is declared in the class in Java is of the reference type and requires that the object be created before they are used, but the static method and static data are loaded into separate memory inside the JVM, called the context that is created when the class is loaded. If the main method is static, it will be loaded in the JVM context and available for execution.

and finally, this: 3. The main method is the entry point for any Core Java program. Execution begins with the main method.

So, in conclusion: Java first charges your main method, public makes this method externally accessible to the JVM, and static sets this method in the context of the JVM, so the first thing the JVM loads on your main method!

 Class Hello{} 

just make your class available to all classes from one package.

0
04 Sep '13 at 15:49
source share

Here is a similar question with a fairly simple answer.

Basically, the JVM can access the core in any class that has either default access or public access, because it is an entry point. If the class is private or protected , you will get a compilation error.

0
Sep 04 '13 at 15:58
source share

when the JVM starts loading the class specified on the command line (see jls java virtual machine start up ) and you cannot have the protected or private specifier in the class, so for you there can only be public or just an empty default , and both of these access specifiers allow access to the class inside the same package. Therefore, there is no need to specify the public keyword for the loadable class.

Hope this is clear.

0
Sep 04 '13 at 16:08
source share



All Articles