Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java

 
Sonu Nigam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we use static in public static void main(string arr[])?
 
Norm Radder
Ranch Hand
Pie
Posts: 1025
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. 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
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"J new", please check your private messages for a final important administrative matter.
[ June 27, 2008: Message edited by: Bear Bibeault ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15362
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic