Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

main() method in java

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read The main Method and The main() Method in Java.
[ March 13, 2006: Message edited by: Chetan Parekh ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic