Why is the main() method declared public and static?

Showing Answers 1 - 24 of 24 Answers

manoj

  • Aug 26th, 2006
 

Main is declared public because JVM accesses this method.

it is declared static because it is instance independent.

  Was this answer useful?  Yes

arun kumar r

  • Aug 31st, 2006
 

main() is declared as public and static because

main() : public

       it should be accessable by java interpreter .

    if it is declared other than public it cannot be accessible by jvm because

 it is only a packagelevel access.

main(): static

     this is the method which calls other methods.

main() method is sharable by all methods & it is not particular to any object or class.

  Was this answer useful?  Yes

Prasoon Srivastava

  • Sep 2nd, 2006
 

main method is declared public coz of it is accessed by the interpreter, which access it out side of the class.so it must b public.main method is the first which is called by the interpreter without having to instanciate a particuler instance of class.static allowes to main is called by the interpreter before any object are made.

  Was this answer useful?  Yes

syeda

  • Nov 20th, 2006
 

its  declared as public coz it should be acessable by interpeter and its static coz it should b invoked b4 any object is created....

  Was this answer useful?  Yes

bihag

  • Mar 17th, 2007
 

main() declared as a static because static is class level .... and main method should run first before any other code get execute ..

  Was this answer useful?  Yes

prahlad tripathi

  • Sep 4th, 2011
 

Public : means it can be accessible to all other classes.
static : main is the entry point of a class. In java everything thing is written in a class.Now when you run java filename on command prompt, loader will load the class and jvm will search the main method to enter into the class. so making the main() as static, will make jvm access it directly through classname.main()

Now once jvm get the main(), object instantiated there are accessed.

Code
  1. public class abc

  2. {

  3. public static void main(String args[])

  4. {

  5. }

  6. }

  Was this answer useful?  Yes

Nikhil Kumar

  • Sep 5th, 2011
 

To all the guys who have given the reason for main method in java being public i have another question.....

The class which declares the main method have package level access because of the default modifier then what is the need of marking the main method as public ?

If anyone knows the answer please mail me.

Thanks in advance.

  Was this answer useful?  Yes

bobby

  • Apr 15th, 2012
 

We declare, main method in java as : public static void main(String args[])

public : means it can be accessible to all other classes.

static : main is the entry point of a class. In java everything thing is written in a class.Now when you run java filename on command prompt, loader will load the class and jvm will search the main method to enter into the class. so making the main() as static, will make jvm access it directly through classname.main()

Now once jvm get the main(), object instantiated there are accessed.

void : main method should return anything.

main : its the method name, the JVM will seek for after the class is loaded on runing command like e.g java classname

String args[] : String array named args used to access the string variables passed on command line. e.g java classname firstname lastname

  Was this answer useful?  Yes

Preetam Singh

  • Feb 6th, 2013
 

We declare, main method in java as : public static void main(String args[])

public : means it can be accessible to all other classes.

static : main is the entry point of a class. In java everything thing is written in a class.Now when you run Java filename on command prompt, loader will load the class and JVM will search the main method to enter into the class. so making the main() as static, will make JVM access it directly through classname.main()
Now once JVM get the main(), object instantiated there are accessed.

void : main method should return anything.

main : its the method name, the JVM will seek for after the class is loaded on running command like e.g java classname

String args[] : String array named args used to access the string variables passed on command line. e.g java classname firstname lastname

  Was this answer useful?  Yes

Nikhil kumar

  • Sep 23rd, 2016
 

Our main method is public because public is modifier which is access to every one for visibility. Our program is for everyone. It should be public static because we are not instantiating static. Our static is no where related to object creation. First, JVM searches static block and then the main method that is why our method is public static.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions