Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Methods

Generally, a program is divided into sub parts to avoid complexity.
Each part is used to perform a specific task. These parts are nothing but methods.

For better understanding, we can consider the example of a house.

Java Programming Language Method Example

For us the house is a single entity. But it is divided into many parts as rooms according to the activities (functions) that we perform in those rooms.
For example, Kitchen room is used for cooking food; Bedroom is used for sleeping, Living room for general purpose activities, Bathroom & Toilet for cleaning activities.
Imagine what would happen if all the activities are done in a single big room. Oh! It’s very tough to imagine.

Similarly in Java programming, writing a large program as a single part makes a program more complex.
In order to avoid complexity, it became necessary to divide a large program into different parts. This is the reason which brought method concept into existence in Java programming.

So in a programmatically view, house acts as a complete program and rooms as its different parts (methods).
Depending on the type of the activity that we want to perform we choose the room (method/part) of the house (program).
Generally, Living room acts as an entrance to a house. Similarly, static main() method acts as an entrance for JVM to access other methods directly or indirectly.

Therefore simply we can say that method is a part of the program that performs a specific task.

Syntax for method

accessModifier returnType methodName(parameterlist)
{
    // programming
    // statements
}

A method is divided into two parts, known as a method header & a method body, as shown below:

Java Programming Language Syntax of Method

accessModifier (Access Modifier)

It is used to decide the access type of the method.
Java’s access modifiers are public, private & protected.
If access modifier is not applied to method then by default it is public within its own package.

returnType (Return Type)

The return type of the method is decided on the type of the value that should be returned by the method.
For example, if the method returns an integer value, its return type must be int. If the method returns double value, its return type must be double.
If the method does not return any value, its return type must be void.

methodName (Method Name)

It can be any name.
But it is better to name the method that is suitable to the task performed by the method.

parameterlist (Parameter list)

It is the list of parameters arranged in sequence with suitable type.
Parameters are used to receive value required for methods operation.
The method can have any number of parameters.
Generally, if there is no need to receive values for the method, the parameter list is kept empty.

Method body

It is the part of the method where the actual task is performed.
It contains the statements required for operations.

Examples of method

Here we have created two methods performing two distinct tasks.
The method with name kitchen is used for performing cooking-related tasks & method with name bedroom for sleeping purpose.

 public void kitchen()
{
    System.out.println("I entered in a Kitchen room");
    System.out.println("I cooked a food");
    System.out.println("I ate food\n");
}

                                                                                 And

 private void bedroom()
{
    System.out.println("I entered in a Bedroom");
    System.out.println("I slept");
    System.out.println("I woke up in the morning\n");
}

Note

Java program can contain any numbers of methods.

Advantages of methods

Makes program simple

It helps the program to divide into different parts instead of writing whole code into one part. It results in avoiding the complexity of the program.

Becomes easy to understand, maintain & debug.

Code reusability

The Same method can be used multiple times at different places. For that, we just need to call the method whenever required.

Method Calling

                             Java Programming Language Method Calling

To execute any certain method in a program, the programmer needs to invoke/make a call to that method using method calling code so that JVM could come to know the method to be executed required by the programmer.

The general form of a method call

methodName(argument list);

The argument list is kept empty if the called method does not receive any value.

Example of method calls without argument

bedroom();

This code is used to call the bedroom method in a program.

kitchen();

This code is used to call the kitchen method in a program.

bathroom();

This code is used to call the bathroom method in a program.

Note

The method that calls another method is known as caller method.
The method that is called by another method is known as a called method.

Following is the Example to explain method working & its calling

public class House 
{
    public static void kitchen()
    {
        System.out.println("I entered in a Kitchen room");
        System.out.println("I cooked a food");
        System.out.println("I ate food\n");
    }
	
    private static void bedroom()
    {
        System.out.println("I entered in a Bedroom");
        System.out.println("I slept");
        System.out.println("I woke up in the morning\n");
    }
	
    public static void bathroom()
    {
        System.out.println("I entered in a Bathroom");
        System.out.println("I got refreshed\n");
    }
	
    public static void main(String args[])
    {
        System.out.println("I entered in a Living room\n");
        bathroom();
        kitchen();
        bedroom();		
    }
}

Output

I entered in a Living room

I entered in a Bathroom
I got refreshed

I entered in a Kitchen room
I cooked a food
I ate food

I entered in a Bedroom
I slept
I woke up in the morning

For the better understanding of the above program look at the diagram shown below:

Java Programming Language Working of Method Call

In the above program, every method is declared as static so they are accessed without any reference to any object.
The main() method acts as an entrance for JVM.
Here main() method acts as caller method because it makes call to bathroom(), kitchen() & bedroom() methods.
Therefore we can say that the method which calls another method is known as caller method.

Small Example showing code reusability

Here the same method bathroom() is used/called again & again.

public class House 
{
    public void bathroom()
    {
        System.out.println("I entered in a Bathroom");
        System.out.println("I got refreshed\n");
    }
	
    public static void main(String args[])
    {	
        House owner = new House();
        System.out.println("I entered in a Living room\n");
        owner.bathroom();
        owner.bathroom();
        owner.bathroom();
    }
}

Output

I entered in a Living room

I entered in a Bathroom
I got refreshed

I entered in a Bathroom
I got refreshed

I entered in a Bathroom
I got refreshed

Accessing methods with references

If methods are declared as static then they are considered as class methods, therefore they can be accessed directly without any reference.
If methods are not declared as static they are considered as instance methods, therefore they should be accessed with the help of class type references, as shown below:
But once program control enters into any one method for specific instance then from that method other methods of the same instance can be called without reference.
The static main() method acts as an entrance for JVM.

Example

public class House 
{
     public void kitchen()
     {
         System.out.println("I entered in a Kitchen room");
         System.out.println("I cooked a food");
         System.out.println("I ate food\n");
     }
	
     private void bedroom()
     {
         System.out.println("I entered in a Bedroom");
         System.out.println("I slept");
         System.out.println("I woke up in the morning\n");
         bathroom(); 
     }
	
     public void bathroom()
     {
         System.out.println("I entered in a Bathroom");
         System.out.println("I got refreshed\n");
     }
	
     public static void main(String args[])
     {		
         House owner = new House();
         System.out.println("I entered in a Living room\n");
         owner.bathroom();   // with reference
         owner.kitchen();    // with reference
         owner.bedroom();    // with reference
     }
}

Output

I entered in a Living room

I entered in a Bathroom
I got refreshed

I entered in a Kitchen room
I cooked a food
I ate food

I entered in a Bedroom
I slept
I woke up in the morning

I entered in a Bathroom
I got refreshed

 

Leave a reply