- 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?

29th Aug 2016, 9:23 AM
Jia Wei Sim
Jia Wei Sim - avatar
4 Answers
+ 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.
29th Aug 2016, 9:27 AM
James
James - avatar
+ 1
we usually keep methods public to allow access from unless there is some restriction to be put
29th Aug 2016, 9:51 AM
tejinder
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
29th Aug 2016, 10:28 AM
Tubai Roy
Tubai Roy - avatar
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
29th Aug 2016, 11:39 AM
ajay kl
ajay kl - avatar