Methods in JAVA

 Methods


 General form of a method definition:

type name(parameter-list) {


... return value;


...

}


 Components:

1) type - type of values returned by the method. If a method does not return any value, its return type must be void.

2) name is the name of the method

3) parameter-list is a sequence of type-identifier lists

separated by commas

4) return value indicates what value is returned by the method.


Example: Method


Classes declare methods to hide their internal data

structures, as well as for their own internal use: Within a

class, we can refer directly to its member variables:


class Box {

double width, height, depth;

void volume() {

System.out.print("Volume is ");

System.out.println(width * height * depth);

}

}


Parameterized Method


• Parameters increase generality and applicability of a

method:

• 1) method without parameters :


int square() { return 10*10; }


• 2) method with parameters :


int square(int i) { return i*i; }


• Parameter: a variable receiving value at the time the

method is invoked.

• Argument: a value passed to the method when it is

invoked.

Post a Comment (0)
Previous Post Next Post