- 1
In java what is the purpose of putting the keyword "public" in main method? Why we do not put the word "public" in user defined methods?
4 Respuestas
+ 1
public is used to allow access to a class/method from outside a package/class. The main method, is called from the JVM, which is outside the default package. If the main was not public, the JVM would not be able to locate it.
+ 1
we usually keep methods public to allow access from unless there is some restriction to be put
0
main method has to be public or else the user cant access any of the members or other classes or methods called by the main method
0
by putting public before main method ,main method can be accesed from any package ,as we know compiler call main method if we dont not use public then compiler wont be able to call it and program wont load,
similarly if u call a method not having public before it from different package using object compiler throw error however it can be called in same package using object ,u have to take a look a bit on access specifiers of java and learn difference between default and public access specifiers