6

Why is the main declared as public and static?

public static void main(String arg[]) {} 

acording to ans in java

"The method is static because otherwise there would be ambiguity: which constructor should be called?" 
1
  • What part of the answer did you not understand? We can't help you if we don't know where you're stuck. Commented Mar 11, 2014 at 14:14

1 Answer 1

45

public - The main method is called by the JVM to run the method which is outside the scope of the project therefore the access specifier has to be public to permit a call from anywhere outside the application.

static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.

void - Java is a platform independent language, therefore if it returns some value, then the value may have a different meaning between different platforms so unlike C it can not assume a behavior of returning value to the operating system.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.