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

main() method in java

 
vigneswar rao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai ,
I hava a small doubt about the main() method .
we are declaring a simple class like


public class Example
{
public static void main(String args[])
{
System.out.println("Hello JavaRanch");
}
}

In the above code , what is the meaning of main(String args[])
Can any one explain...........
cheers
vigneswar.
 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String[]args)

Is an entry point to your java application.The excecution of the program starts from main.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read The main Method and The main() Method in Java.
[ March 13, 2006: Message edited by: Chetan Parekh ]
 
Guy Shahar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method is like any other method, except that it is the one that will run automatically when you call your program.

Each method can take "arguments" which are listed in brackets after the name of the method. These arguments can provide information for the method to use.
For example - public void printSomething(String s){System.out.print(s);}
When you call this method, you may type in:
printSomething("Hello");
and the method with print the word "Hello" on the screen.

The main method takes as an argument an array of Strings, which is traditionally given the name args, though it could be called anything (for example, "main(String[] potatoCakes)" would be valid aswell). The array comes from anything that the user types AFTER the name of the class at the commandline. Normally, to start an application, you would type:
java MyApp
And anything you type after that is an item in the String array. So if I type:
java MyApp Potatoes Cakes PotatoCakes Mmmmm
Then the String[] args array would contain 4 elements:
args[0] = "Potatoes"
args[1] = "Cakes"
args[2] = "PotatoCakes"
args[3] = "Mmmmm"
and these can be used within the main method by referring to their reference in the array. e.g. System.out.print(args[1]);
 
Yu Fei Xu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
the main() method just the method that the program get to enter,
you can use the parameter like the other methods you support,
the parameter in main() is a string array,when you compiler your
program, you should support the parameter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic