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

Answer Posted / sanjay gupta

public static void main()

1> Main() is the entry point of the program. Rather we can
say it is the main gate to enter inside the apartment. It
should be public because the compiler need to enter inside
the method (The guest need to access the apartment to reach
the security guard).

Public--> Compile whos is from another instance need to
access this method.

Statis --> Since main method is written inside a class and
to access a method of a class we need to create an object
of that class first. Since main() is the method which
compiler nned to call before creating any object of that
class we need to declare main as static. Static methods can
be called directely by using the class name. Thats why the
neame of the file should be same as the name of the class
contain main() method.

Void--> Since the method can return any type of valu or it
might not return anything also so the comiler is designed
in such a way that it should not take any return value so
we declare main as void type.

For further information please reach me at
sanjaygupt1011@gmail.com

Thanks~
Happy Programming...

Is This Answer Correct ?    88 Yes 12 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does 0 mean in boolean?

544


What is the base class for error and exception?

556


Is ruby built on java?

535


What is default switch case? Give example.

578


Can java list be null?

554






What is synchronization and why is it important in java programming?

470


What is return keyword in java?

567


What interface is extended by awt event listeners?

595


What package is math in java?

536


What is set string?

596


Write a java program to check if a number is prime or not?

537


What is abstract class? Explain

597


How do you reverse a string in java?

583


What does java final mean?

522


What do you mean by platform independence of Java?

538