In Java why we write public static void main(String args[])
why not main()?

Answer Posted / angela

when we run java program, jvm internally calls "main" method.
jvm is built in such a way that it searches for the entire
signature of "main" method i.e public static void
main(String a[])

Example program :
----------------------
class Demo
{
public static void main(String a[])
{ }
}

String a[] -> is used to pass values at runtime.
(c:/>java Demo hai)

public -> providing accessibility for outside code (jvm).

static -> without creating an object calling "main" method
with the class name

c:/>javac Demo.java
c:/>java Demo
(here java internally calls jvm and passes class name
Demo, then jvm loads Demo class and calls Demo.main()
without creating object )

void -> does not return any value because jvm simply calls
main() method and does not have a variable to assign return
value.

Is This Answer Correct ?    23 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are scriptlets?

601


Difference between java and javascript

586


Is array a class?

523


What is class array in java?

517


Can You Have Virtual Functions In Java?

590






Can we access instance variables within static methods ?

573


Can we have 2 main methods in java class?

543


What is final variable?

501


How do you compare objects in java?

505


What is a method vs function?

564


Difference between method overloading and overriding.

582


Explain about main() method in java ?

588


What is implicit object in java?

554


Why is it called buffering?

585


Can we convert stringbuilder to string in java?

562