1

I'm new to java (today was my first lesson). I tried to read and do a small exercise but I don't understand exactly what the main method is.

Our teacher told us to just focus on the main method and not more, but he did not explain what it is. He just said it is the start of a program in Java. I would like to understand more, but it's difficult because every time i encountered difficulties. Example:

public static void main(String[] args) {

}

Why does this method exist? Why can't I choose another name?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • Welcome to [so]. You might want to take a look at [The Java Main Method](http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html). There are a plethora of articles out there about these sorts of things though, both here and off-site. It's better to take the time to do the research before asking, and it will serve you well to know how to do so in the future, without anyone else telling you how. :) – Qantas 94 Heavy Dec 13 '13 at 04:22
  • 3
    Please add an upper case letter at the start of sentences. Also use a capital for the word I, and abbreviations and acronyms like JEE or WAR. This makes it easier for people to understand and help. – Andrew Thompson Dec 13 '13 at 04:27
  • and this: http://stackoverflow.com/questions/19211288/java-programming-main-method-in-class?rq=1 – Ross Dec 13 '13 at 04:27

5 Answers5

0

Because from main method the program starts .As many you have main() mehtods as many you have programs.This is the starting point of any program

http://docs.oracle.com/javase/tutorial/getStarted/application/

http://csis.pace.edu/~bergin/KarelJava2ed/ch2/javamain.html

http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/main.html

Deepak
  • 2,287
  • 1
  • 23
  • 30
0

Within Java (and many other languages) the main function is special, because it is the Entry point. I suggest you read this document, with a focus on the section beginning with "The main Method".

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

The main method is your entrance to the program, that's it. It's where you start from.

You don't have to know the specifics of what each keyword does yet, the only important thing for you is to realize it has a parameter called args of type String[]. This is what allows your program to take arguments when executed.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

Welcome to java :)
i try to use the most simple word. to reply at your question:
the main method is called by the system when you run your program, for this reason you have to use this name (main), because when your app start there is someone that call by default the method main. if you choose another name... you cannot run your program because when the system (i call it system because i think you need to read a little bit) call the main method, if it cannot find it you cannot start your program.

try to think:
have to someone that have to start your program right? but how it can know from where your program have to start ? for this reason java (but also other language) decide that the begin is the method main.

Giovanni Far
  • 1,623
  • 6
  • 23
  • 37
0

A java program is a set of method containing at least one method. This is the method by which the program begins to run. Method Main.

It must be declared as :

public static void main(String[] args) {
    // Your code
}