6

I wrote the following code

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

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

However, if JVM spec mandates that main method should be public since "it can't see main otherwise", shouldn't it apply to the class as well? If the JVM "can't see" Hello.main() when it is not declared public, how is it able to see the class A itself.

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

And if the JVM is able to see all classes and methods as it is the "security/visibility enforcer" itself then why does the main method needs to be declared as public.

Zaid Khan
  • 786
  • 2
  • 11
  • 24

8 Answers8

10

Just for kicks, a demo that private classes can also hold main:

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

Compiles and runs fine from the command line:

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

C:\junk>

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
4

if JVM spec mandates that main method should be public since "it can't see main otherwise"

It can see but it doesn't see it as the entry point and that is why it gives NoSuchMethodError: main if you try to execute a class having no such method.

By classic design, the main entry point-

  1. Must be named main
  2. Must be public
  3. Must be static
  4. Must be void
  5. Must have one argument that is an array of string

Hence,

public static void main(String args[])

Being static, JVM can call it without creating any instance of class which contains the main method. Not sure if it is the main reason for main being static by design.

A class with default access like Hello in your example is only visible to other classes in the same package.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
1

I don't think the specification says that the class has to be Public. Refer to the examples on the official java tutorial. None of the classes with main method in the examples are declared as Public.

This was discussed previously on stackoverflow. Refer: Package-private class within a .java file - why is it accessible? Explains it well.

Community
  • 1
  • 1
Anurag Kapur
  • 675
  • 4
  • 19
  • Thanks for the reply. My question was the that the JVM requires the main() to be public for it to be accessed from anywhere. But if the class containing the main() is not public then what's the purpose of main() being public(i.e its visibility is restricted by that class) – Zaid Khan Sep 05 '13 at 16:55
0

Mind that main is an early language feature. My guess is that it was thought that a private method could disappear in the .class file, maybe inlined, maybe given a shorter name. So it is a simple overrestricting (?) convention for retrieval of the correct method:

static void main(String[])
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

the default access specifier is package.Classes can access the members of other classes in the same package.but outside the package it appears as private but JVM has access to all the classes thus JVM can alter the visibility just to find the beginning of the program thus it makes it default by default

0

Of so first let's consider this 1. Since main method is static Java virtual Machine can call it without creating any instance of class which contains main method.

this: 2.Anything which is declared in class in Java comes under reference type and requires object to be created before using them but static method and static data are loaded into separate memory inside JVM called context which is created when a class is loaded. If main method is static than it will be loaded in JVM context and are available to execution.

and finally this: 3. Main method is entry point for any Core Java program. Execution starts from main method.

So in conclusion: Java first charge your Main method, the public make this method accessible from everywhere for the JVM, and static set the method in the JVM context so the first thing that the JVM loads it's your main method!

Class Hello{}

just make your class accessible to all classes from the same package.

Abstract
  • 664
  • 1
  • 5
  • 15
0

Here's a similar question with quite a straightforward answer.

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

Community
  • 1
  • 1
0

when JVM starts it loads the class specified in the command line (see the jls java virtual machine start up), and you cannot have protected or private specifier in the class so the only option for you is to either have public or just blank default and both these access specifier allows the class to be accessed inside the same package. So there is no need for specifying public keyword for the class to load.

Hope its clear.

Sajan Chandran
  • 11,287
  • 3
  • 29
  • 38
  • Do you believe that the reason main must be declared public is for it to be accessible by the JVM OR it's just part of the syntax like the java developers wanted us to follow their syntax! – Zaid Khan Sep 07 '13 at 12:09