Home » Online Computer Education » Learn Fundamentals of Java Programming “User Defined Methods in Java Programming ” Lesson 16

Learn Fundamentals of Java Programming “User Defined Methods in Java Programming ” Lesson 16

User Defined Methods

A method in Java is a block of statements grouped together to perform a specific task. A method has a name, a return type, an optional list of parameters, and a body. The structure of a Java method is as below:

return_type method_name(list of parameters separated by commas) {
statements
return statement
}

The method name is a word followed by round brackets. The parameter list placed within the round brackets allows data to be passed into the method. This list is a comma separated list of variables (as many as needed) along with their data types. The variables of the parameter list act just like regular local variables inside the method body. The method body is enclosed within a pair of curly braces and contains the statements of the method. The statements end with an optional return statement that returns a value back to the calling method. The return type is the type of the value that the method returns.
Let us write a method that given the length and breadth of a rectangle as parameters returns the area of the rectangle.

static double static double rectangle_area(double length, breadth)  {
return (length * breadth );
}

The name of the method is rectangle_area. The method has two parameters – both of type double. The body of the method has only a single statement, one that calculates the area based on the parameters and returns it. The return type from the method is of type double. We have declared this method as a static method. The static modifier allows us to call this method without an invoking object. (You will learn more about objects later).
A method is called/invoked from another method. When a method is called, control is transferred from the calling method to the called method. The statements inside the called method’s body are executed. Control is then returned back to the calling method.
Let us call/invoke the rectangle_area method from the main method.

public st atic void main(String[] args) {
double a = 0;
a = rectangle_area( 45.5 , 78.5 );
System.out.println(” Area of the rectangle = “+ a );
}

When we call the rectangle_area method, we supply the length and breadth of the rectangle as arguments in the method call. The arguments with which the rectangle_area method is called are copied into its parameters. In this case, 45.5 is copied into the parameter length and 78.5 is copied into the parameter breadth .

double rectangle_area(double length, breadth)
double a = rectangle_area( 45.5 , 78.5 );

The method is executed and the return value from the method is copied into the variable a. In this case, a gets the value 3571.75, the product of 45.5 and 78.5.

double rectangle_area(double length, breadth)
3571.75
double a = rectangle_area( 45.5 , 78.5 );

The System.out.prinltn System.out.prinltn () then displays the area in the output window.

Area of the rectangle = 3571.75

We can call the rectangle_area method with different arguments and we will get a different result.

Figure 1.6a shows the complete program listing of the MethodDemo program and Figure 1.6b displays the output from the program.

Java fundamentals  array 3

A special return type void can be used if a method does not return any value. For example, a method that just prints a string need not have a return value can us evoid as the return type.

void print_message (String message ) {
System.out.println(“The message is: “+ message);
}
This method can be called with a statement such as
print_message(“This is a secret message!”);

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *