Monday, May 05, 2008

Methods

Introduction

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.

Program

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

Discussion

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: