Tuesday, November 17, 2009

Java's Use of Methods

In most programming languages, the use of modular approach to solve programming works is simply natural and inherent.

In Java, this is realized through the use of methods. The use of methods, allows the class where the methods are defined to reuse it the nth time. Further, other classes outside the defining class can also call on these methods.

In using methods, one should observe the following:
1. method header
2. use of correct arguments
3. body of the method

Methods may also return a value or not. This arrangement should be properly defined as we define methods.

Check the sample program to illustrate the use of methods.

[+/-] show/hide

public class WeightMethod {

public static void main(String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(;
double weightpds=0.0;

String str;

System.out.println("Please enter your weight in pounds:");

weightpds=Double.parseDouble(str); //convert string to double

System.out.println("Your weight of "+weightpds+" pounds is "+Compute(weightpds)+" kilos."); // the method Compute was invoked or called

// the method definition with its argument and returned value
public static double Compute(double weightlbs){

double weightkls=weightlbs/2.2;
return weightkls;