17

If we declare the class with default scope(non-public) and public main method it executes successfully. Here class scope is mote stricter than main method scope.

But if we declare main method as default, then JVM will throw error. Why?

class DefaultTest {
    public static void main(String[] args) {
        System.out.println("output.........");
    }
}

Runs successfully but

class DefaultTest {
    static void main(String[] args) {
        System.out.println("output.........");
    }
}

this won't.

I mean if the class itself is not public JVM can still access main method that means there is no need of main to be public. But if we don't declare it as public, it will throw an error.

Ninja
  • 2,050
  • 1
  • 23
  • 27
  • @assylias 7 days ago xD – nachokk Dec 25 '13 at 20:34
  • 2
    I mean if the class itself is not public jvm can still access main method that means there is no need of main to be public. But again if we don't declare it pubic it will throw error. – Ninja Dec 25 '13 at 20:42
  • to access main method by JVM from out side the class and package and some other location or network. you can find more explantion here: http://javabynataraj.blogspot.in/2014/05/why-public-static-void-mainstring-args.html – Mdhar9e May 02 '14 at 11:44

9 Answers9

11

It is specified by the Java Language Specification, see http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html chapter 12.1.4. Invoke Test.main:

The method main must be declared public, static, and void.

It is also required by the JVM specification, see the answer from @A4L.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • While this answer is true it is same as "it has to be `public` because it has to be public (since JLS says so)". It would be nice if you add info about what bad could/would happen if we let `main` not to be `public`. – Pshemo Dec 25 '13 at 20:58
  • 3
    Technically there is no reason - you could implement a jvm launcher which is able to even launch a private method – Andreas Fester Dec 25 '13 at 21:06
10

static so that the JVM can run the method without having to instantiate the class object + public so that the JVM can access it freely without any access issues.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • 1
    dude, if you read the rest, i mention that too. just try to give more info to the person who asked. it seems you have hard time to read :D – Kick Buttowski Dec 25 '13 at 20:27
  • .It is k, i have the same issue. lol. it was kind of my fault cuz i did not have a good format. I will fix it. :) – Kick Buttowski Dec 25 '13 at 20:29
  • 8
    If the class itself is not public jvm can still access main method that means there is no need of main to be public. But again if we don't declare it pubic it will throw error. – Ninja Dec 25 '13 at 20:54
  • 1
    That really does not explain the reason of main method `public`. –  Sep 13 '16 at 03:33
  • it did't helped at all -_-.. – Raj Kumar Aug 25 '19 at 16:45
9

From the jvms

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

So it is specified this way and jvm vendors has to respect it!

A4L
  • 17,353
  • 6
  • 49
  • 70
6

I think it is just a rule in the Java language.
Technically I don't see another reason (because even if
it's not public the JVM can still find it and call it).

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • 2
    I think the answers of A4L and Andreas support my opinion. It is just a convention prescribed by the JLS, not a strict necessity. But yes, if non-public was allowed for the main method, that would be ugly and kind of illogical. – peter.petrov Dec 25 '13 at 20:25
  • 1
    Exactly - Unfortunately I do not have an official source which proves it, but I think that this is what the language designers had in mind. So, IMHO the only provable answer is: the jls specifies it, so implementors have to follow it ... – Andreas Fester Dec 25 '13 at 21:16
1

Main method has to strictly follow its syntax; other wise JVM will not be able to locate it and your program will not run.

They are public because they must be accessible to the JVM to begin execution of the program.If it is not public then only the class and the package members can access it and JVM cannot.

Main is the first method that would get executed in any class. They are static because they must be available for execution without an object instance.

GingerNinja23
  • 162
  • 2
  • 11
0

If no modifier (e.g., public) is used then only the class and the package can run the method. This means that the outside world can't run the main method. That, in turn, is why you need to define the method with public.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • The thing is that usually the JVM runs this method. So... Isn't this just a rule? Is it needed technically? – peter.petrov Dec 25 '13 at 20:19
  • 1
    @peter.petrov Everything about visibility is "just a rule". In this particular case, as it's not other Java code who runs the `main`, it's not really necessary, but conceptually it makes clear that *everyone* can run it. – Darkhogg Dec 25 '13 at 20:21
  • @Darkhogg Well, "conceptually it makes clear that everyone can run it" Conceptually - yes, but technically I don't see a reason for it to be public. I thought this was what the OP was asking. – peter.petrov Dec 25 '13 at 20:22
  • @Darkhogg "Everything about visibility is just a rule" I wouldn't say so. I mean, if it was to be called from Java code then OK, it must be public. But as it's called from the JVM - I don't see how public is really a must. You see my point. – peter.petrov Dec 25 '13 at 20:23
  • @peter.petrov Technically there's also no reason why it has to becalled `main`. In fact, if you use applets, you don't define a `main` method. It's just a convention. – Darkhogg Dec 25 '13 at 20:24
  • @Darkhogg Yeah, right, we're on the same page. – peter.petrov Dec 25 '13 at 20:29
  • If the class is not public jvm can still access main method that means there is no need of main to be public. But again if we don't declare it pubic it will throw error. – Ninja Dec 25 '13 at 20:57
0

Java specifies several access modifiers e.g. private, protected and public. Any method or variable which is declared public in Java can be accessible from outside of that class. Since main method is public in Java, JVM can easily access and execute it

Garini
  • 1,088
  • 16
  • 29
0

Public classes and method can be accessed by anyone whether within or out of the package. So since jvm iyself has class loaders in different package than the default or your package, you need to put the main with public access modifier

0

because main method is gateway, from where your project can be access. If you make private this main gateway to your application then accessibility of functionality is not possible.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110