wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes why public static void main(String args[]) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why public static void main(String args[])" Watch "why public static void main(String args[])" New topic

why public static void main(String args[])

John Chong

Joined: Jan 18, 2001
Posts: 16
May I know why the word static? And is args[] used to capture command line input like javac myfile.java kkk?
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
static is a keyword in the java language, and it is also a modifier. When a method or variable is declared static, it means that it can be used throughout the whole class without instantiating an object. If it isn't static, then you must create an object to invoke the non-static method or non-static variable. You are correct about args[]. For instance say I had a program called Go. When I use the command java Go one two
arg[0]= one , arg[1]= two.
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
static is not used to catch anything its an access modifier which tells that inside this method you don't need to create any instance of an object to execute it. As for a parameter it takes an array of strings because you might pass certain arguments to your program to let it know what to do. Special conditions that you might think of.

Val SCJP <BR>going for SCJD
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340

Static is a modifier but not an access modifier. When a member is static it means that no matter how many instances of the class you have, there is only instance of the static member which they all share.

Visit my download page
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
I agree. Here's the link: http://aspose.com/file-tools
subject: why public static void main(String args[])
Similar Threads
Signature of main() method
static Again...
Static Variable Problem
why static there?
What is the main usage of "String[] args" in public static void main(String[] args)?