5

I declared the following class

class A { //not public
  public static void main(String args[]) {
     System.out.println("done");
 }

When I compile and run it, it runs fine and prints the output "done". Same behavior even when I declare it as being in a "package a;"

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" A.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"?

Jagat
  • 1,392
  • 2
  • 15
  • 25

3 Answers3

4

The JVM has access to every class in the application all the time because one of its responsibilities is enforcing visibility rules. Therefore, one can draw the conclusion that it can ignore visibility rules if need be (e.g. when the user starts the application, the JVM has to find the entry point, which is main()).

In other words, the JVM is not a class accessing this function, so visibility doesn't apply. It is basically the overseer, managing the application from execution to termination.

For reference, see Execution.

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • if "*visibility doesn't apply*", why does the **main method** needs to be `public` for the JVM to access it, or is it just convention to set it to `public`. – dumbPotato21 May 07 '17 at 04:15
  • 1
    @Shashwat That is a convention enforced by the JVM. See http://stackoverflow.com/a/11444861/483349 – Evan Mulawski May 08 '17 at 13:29
0

When you declare a class private, you're not making it "invisible", and the same goes for your methods. Declaring a method private simply means it's not callable from outside your class. A static public method of a private class is publicly callable.

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
  • From what I've learnt, a package private(default access) class is essentially invisible to any class outside that package. So its public class is not usable to a class outside that package. – Jagat Mar 09 '12 at 20:25
0

The reason the JVM can see a non-public class is because it controls visibility, meaning it sees everything and decides what can see/call/access what.

The use of public on a class is different than on a method, but the concept is the same.

On a method, the public keyword means the method can be used outside the class. An example would be:

class A {
  public static void do() {
    // Do something
  }
}

class B {
  public static void main(String[] args) {
    A.do(); // This works because do() is public and static
  }
}

The same concept applies to classes, but in a different way.

Using public on a class means that it can be used outside the current .java file (it will have its own .class file).

Here's an example:

//C.java

  class C {
    static void do() {
      // Do something
    }

    public static void run() {
      A.do();  // Works because A.do() is public and static
      B.do();  // Does not work because B is not a public class
    }
  }



//A.java

  public class A {
    public static void main(String[] args) {
      B.do(); // Works because B is in the same file
      do();   // Duh...
    }

    public static void do() {
      // Do something
    }
  }

  class B {
    static void do() {
      // Do something
    }
  }
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62