Methods in Java


This post is targeted to introduce you to methods in Java. By the end of this article, you'll not only know what a method is, but you will be able to understand its Java syntax, why we need it and how you can write a method for your requirements.

What is a method?

A method is composed of a certain number of lines of code, written to perform a specific operation.

For example, you can write a method to get the sum of two numbers. Similarly, when you print a line on the console, the line System.out.println(x); is a method provided by Java to print content. In Java syntax this command means, you want to print x on screen, using the println() method.

Why do we need to use methods?

If you are supposed to divide two numbers, you can write a snippet of code to divide them.

Example: Method in Java
public final class MethodsInJava {
	public static void main(String[] args) {
	    int dividend = 10;
	    int divisor = 5;	
	    int quotient = dividend / divisor;
	}
}

Let's say, you are given to divide two numbers 100 times. Then before writing that snippet 100 times, you might get frustrated by even the thought of it.

Instead of writing redundant lines, if we can write a block of code that you can re-use 100 times. How would that sound? It will not only save us a lot of time, but it will also reduce the memory consumed, increase code reusability, divide your program into small independent units (modularity) and lead you to a more sophisticated approach.

How can I write my first method?

Continuing the example of dividing two numbers, let's see how you can write a method to divide two numbers. And how you can re-use it for various numbers.

Example: Method in Java
public class MethodsInJava {

	public static int getQuotient(int dividend, int divisor) {
		return dividend / divisor;
	}

	public static void main(String[] args) {
		int x = 20, y = 2;
		System.out.println( x + " / " + y + " = " + getQuotient(x, y));
		
		x = 30; y = 5;
		System.out.println( x + " / " + y + " = " + getQuotient(x, y));
		
		x = 12; y = 6;
		System.out.println( x + " / " + y + " = " + getQuotient(x, y));
		
		x = 45; y = 8;
		System.out.println( x + " / " + y + " = " + getQuotient(x, y));	
	}
}
Output
20 / 2 = 10
30 / 5 = 6
12 / 6 = 2
45 / 8 = 5

Explanation

public static int getQuotient(int dividend, int divisor) {

  • public is an access modifier. It means you can access this method (referred to as function sometimes) outside the class MethodsInJava too.
  • The keyword static notifies that the method getQuotient can be called even without creating an object of class MethodsInJava. This is a concept related to object-oriented programming (you can skip it for now).
  • int is the return type of the method. This means, after executing the lines of code, you can return an instance of any data type if you want to. Or keep it void if not required. In this case, the product is returned of type int.
  • getQuotient the name of the method.
  • (int dividend, int divisor) are parameters passed to the method, of type int. You can pass multiple parameters to a method according to your requirements.
  • { This curly brace marks the beginning of the method.
  • return dividend / divisor; } returns the quotient by dividing the given parameters.
  • } marks the end of the method.
  • main() is the driver function where you can call the method getQuotient(dividend, divisor) multiple times by-passing any two integers of your choice. You can write the same method for other data types like double, long etc too.

Let's look into another example of writing a method to find multiples of a number.

Example: Method in Java
public class MultiplesUptoN {
	public static void printFirstTenMultiples(int num) {
		System.out.println("Printing first 10 multiples of " + num);
		for (int i = 1; i <= 10; i++) {
			System.out.println(num + " x " + i + " = " + num*i);
		}
	}
	public static void main(String[] args) {
		// printing multiples of 2 different numbers
		printFirstTenMultiples(5);
		printFirstTenMultiples(3);
	}
}
Output
x inside function: 20
Printing first 10 multiples of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Printing first 10 multiples of 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

Conclusion

By now you should be clear that we aim to put one complete functionality in one method. After writing your methods you should test them thoroughly for your desired outputs. Or else, if you have consumed them in other functions too, they will impact all the results. So we advise you to practise this concept thoroughly and come back to this article whenever you get stuck. Good luck and happy coding! :)