Monday, May 05, 2008



A method is a named block that can be called, and where program execution can continue. Other names for methods in non-object-oriented languages are functions and procedures. But unlike functions and procedures, methods are also members of a class.


1 package sharingjava;
3 public class Example {
5     public static void main(String[] args) {
6         System.out.println("Hello World!");
7     }
9 }


In the example program, the main method starts at line 5. The main method is a method that serves as a starting point for program execution. The main method is called automatically when program execution begins. You will get a runtime error if this method is missing.

One more thing... It's impossible to know that your program need the main method just by examining the grammar. You will not find it there.

Here is the Java grammar for writing methods:

MethodDeclaration =
MethodHeader, MethodBody ;

No comments: