File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java" Watch "java" New topic


Sonu Nigam

Joined: Jun 22, 2008
Posts: 2
why we use static in public static void main(string arr[])?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 694
When the java command starts a program, it needs a FIXed way to call it.
By making main() static, it can call main() and let main() do what is needed to instantiate the class and get it started.
J New

Joined: Jun 27, 2008
Posts: 5
.. because the main{String []args)method has to be called by OS.So in order to make it possible for OS to access it without creating the instance of the class, containing the main(String []args)method,it is declared static.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

"J new", please check your private messages for a final important administrative matter.
[ June 27, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

siddharth, welcome to JavaRanch.

Please use a meaningful subject line when you post a question, instead of a title like "java". A better title would have been, for example: "Why is main() static?".

To answer your question: It's really just a design decision by the people who invented the Java language.

By making main() static, the JVM doesn't have to instantiate the class that the method is in to be able to call it - which makes starting a program simpler.
[ June 27, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: java
It's not a secret anymore!