Java – Method

In Java, methods are a group of statements that perform a specific task. In this tutorial, we will learn what is method, how to define the method in Java, and how to use methods with lots of examples.

What is a Method

Suppose, we need to write some fixed lines of code regularly to perform a task, instead of writing it again, and again we will create a method and call it whenever it required. In other programming languages, it is also known as a function. Java methods must be inside the class and it defines the behavior of an object.

  • It is a block of code.
  • It can take input as a parameter.
  • A method can return a value as result.

Types of Method in Java

  • Library method – The predefined methods in java is known as a library method. One example of a predefined method is in the String chapter.
  • User-defined method – We can also create a method for a specific purpose, that is known as user-defined.

How to Define a Method

Let’s start with an example and after that, we will discuss the syntax of the method in Java.

public void msg()
{
	System.out.println("Hello World");
}

In the above example, the name of the method is msg() and it is an example of a method without argument.

Method Without Arguments

We create a method without parameter(argument).

class Demo
{
	//method without argument
	public void msg()
	{
		System.out.println("Hello World");
	}

public static void main(String arg[])
{
	Demo d=new Demo();
	//calling method
	d.msg();
}
}
Output:
Hello World

Method With Arguments

Whenever we pass a value as an input to the method, it is known as an argument. We must specify the type of argument at the time of method definition and must pass the value as a parameter at the time of calling a method.

import java.util.*;
class Demo
{
	//method with argument
	public void testNum(int n)
	{
		if(n%2==0)
		System.out.println("Even Number");
		else
		System.out.println("Odd Number");
	}

public static void main(String arg[])
{
	Demo d=new Demo();
	
	//user input
	Scanner in=new Scanner(System.in);
	System.out.print("Enter a Number: ");
	int n = in.nextInt();

	//calling method
	d.testNum(n);
}
}
Output:
Enter a Number: 5
Odd Number

Method With Arguments and Return Value

In Java, a method can accept input as a parameter and return a value as result. We must specify the return type of result in the method definition. Now, we can use the result later in our program for a purpose.

import java.util.*;
class Demo
{
	//method with argument and return a value
	public String testNum(int n)
	{
		if(n>0)
			return "Positive";
		else if(n<0)
			return "Negative";
		else
			return "Zero";
	}

public static void main(String arg[])
{
	Demo d=new Demo();
	
	//user input
	Scanner in=new Scanner(System.in);
	System.out.print("Enter a Number: ");
	int n = in.nextInt();

	//calling method
	String msg=d.testNum(n);
	System.out.println(msg);
}
}
Output:
Enter a Number: 11
Positive

Method Overloading

In java, a class can have more than one method with the same name, but with different parameters(different numbers or different types). These methods are known as method overloading.

How to Overload a Method

In Java, we can overload methods in 3 different ways.

1. Different Number of Arguments

A method can overload by the different numbers of parameters but the method name should be the same.

addNum(int a, int b)
addNum(int a, int b, int c)

Example: Java example of method overloading with a different number of arguments.

class Demo
{
	//method with two arguments
	public void addNum(int a, int b)
	{
		System.out.println(a+b);
	}

	//method with three arguments
	public void addNum(int a, int b, int c)
	{
		System.out.println(a+b+c);
	}

public static void main(String arg[])
{
	Demo d=new Demo();
	
	d.addNum(10,20);
	d.addNum(10,20,30);
}
}

2. Different Data Types of Argument

If, more than one method has the same name but different data types.

addNum(int a, int b)
addNum(double a, double b)

Example: Java example of method overloading with different data types.

class Demo
{
	//method with integer arguments
	public void addNum(int a, int b)
	{
		System.out.println(a+b);
	}

	//method with double arguments
	public void addNum(double x, double y)
	{
		System.out.println(x+y);
	}

public static void main(String arg[])
{
	Demo d=new Demo();
	
	d.addNum(10, 20);
	d.addNum(2.2, 3.1);
}
}

3. The Order of Arguments

We can overload the method by changing the order of the arguments.

msg(String name, int age)
msg(int age, String name)

Example: Java method overloading example with the order of arguments.

class Demo
{
	//method with String and integer arguments
	public void msg(String name, int age)
	{
		System.out.println("Hello "+name+" you are "+age+" old");
	}

	//method with integer and String arguments
	public void msg(int age, String name)
	{
		System.out.println("Hello "+name+" you are "+age+" old");
	}

public static void main(String arg[])
{
	Demo d=new Demo();
	
	d.msg("Jatin", 22);
	d.msg(23, "Rohan");
}
}