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

How to print an arraylist in java?

491


How do I remove a character from a string in java?

494


Class c implements interface I containing method m1 and m2 declarations. Class c has provided implementation for method m2. Can I create an object of class c?

1012


Implementations of set interface?

558


What is pass by value?

508






How we create object in copy constructor?

638


Does java have extension methods?

525


What is the final blank variable?

584


How does finally block differ from finalize() method?

583


What is use of functional interface in java 8? Explain

565


How do you compare two strings lexicographically?

533


What happens if we don’t override run method ?

562


Is stringwriter thread safe?

550


How can an object be unreferenced?

538


Explain about fail fast iterators in java?

557