Table of contents
1.
Introduction
2.
Methods
3.
Types of Methods
4.
Components Required to Create Java Methods
4.1.
Access Modifier 
4.2.
Return Type
4.3.
Method Name
4.4.
Parameters List
4.5.
Exceptions
4.6.
Method Body
5.
Examples
5.1.
Code 1
5.2.
Code 2
5.3.
Code 3
6.
Frequently Asked Questions
7.
Key Takeaways
Last Updated: Mar 27, 2024

Types Of Methods

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be learning about types of methods before learning the types of methods. We need first to revise what a method is.  A Java method can be expressed as logical statements written together to perform a specific task. A method makes our code less complex and more presentable by reusing our code. When we write code for some application development, our code becomes lengthy. To overcome this problem, Java gives the freedom to make methods to tackle these problems.

Recommended topic about-  Iteration Statements in Java, and Duck Number in Java.

See more, Solid Principles in java

Methods

A Java method can be defined as many logical statements written under a code block to perform some task. These are the critical building blocks of Java applications. In Java programming, a method is executed when called from another method.

 Syntax 
 

method_modifier return_type method_name(Parameter_list) throws Exception 
{
	// Method body
}
You can also try this code with Online Java Compiler
Run Code

 

Must Read Static Blocks In Java.

Types of Methods

  • Build-in Methods: These are predefined methods in the Java library and do not need to be created. Some examples are the max() ways present, which take two inputs and return the one greater than the other.
     
  • User-Defined Methods: These types of methods are needed to be created by the developer.

Components Required to Create Java Methods

Access Modifier 

In Java, there exist four types of access modifiers.

  1. Public: These types of modifiers came up with the maximum accessibility. These can be used in the same class and within the same package. These are also accessible outside the package and also in the subclass of the outside package.
     
public void msg(){
	System.out.println(“Hello Coders”);
}
You can also try this code with Online Java Compiler
Run Code


2. Protected: If a method is declared as protected then these can be accessed within the class along with in the same package and also outside the package by subclass only. These are not accessible outside the package.
 

protected void msg() {
	System.out.println(“Hello Coders”);
}
You can also try this code with Online Java Compiler
Run Code


3. Private: These types of modifiers can be accessed within the same class. These are not accessible to other classes within the same package and outside the package of the subclass too.
 

private void msg() {
	System.out.println(“Hello Coders”);
}
You can also try this code with Online Java Compiler
Run Code


4. Default: If you don’t mention any specifier, then this will be treated as the default one. This default modifier is accessible only inside the same package not outside the package. It provides more accessibility than private.
 

void msg(){
	System.out.println(“Hello Coders”);
}
You can also try this code with Online Java Compiler
Run Code

Return Type

This consists of the data type of the value which can be returned from the function. If the return type consists of the void, it does not return anything.

Method Name

This refers to the method's name, either unique or non-unique. It should follow camel case notation.

Parameters List

This list includes all the input parameters separated by commas, along with their data types. 

Exceptions

This may throw one or more than one exception. We can use more than one exception separated by commas. To learn more about exceptions, you can visit Exception Handling.

Method Body

This represents a block of code enclosed between the braces. This body contains one or more logical statements to perform the task.

You can also read about the topic of Java Destructor and Hashcode Method in Java.

Examples

Code 1

public class Ex14 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		 
	System.out.println("Square root of 16: " +Math.sqrt(16)); 
	} 
}
You can also try this code with Online Java Compiler
Run Code

Output

The square root of 16: 4.0
You can also try this code with Online Java Compiler
Run Code


In this code, we have used an in-built method called sqrt. This method is used to find the square root of any number. The modifier here is used is public to call the square root function.

Code 2

public class Ex15 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		int num1 = 10, num2 = 5, maxValue; 
		maxValue = Math.max(num1,num2); 
		System.out.println("Max value: " +maxValue); 
	} 
}
You can also try this code with Online Java Compiler
Run Code

Output

Max value: 10
You can also try this code with Online Java Compiler
Run Code


In this code, we have used an inbuilt function of getting the maximum number of the given two numbers. The modifier here is used is public to call the maximum function.

Also check out Addition of Two Numbers in Java here.

Code 3

public class Ex16 {

	int a=20; 
	static int c = 40; 
	static int d = 50; 
	void m1() 
	{ 
		
		System.out.println("Third number c = " +c); 
		System.out.println("Fourth number c = " +d); 
	} 
	static void multiply() 
	{ 
		int mNum = c*d; 
		System.out.println("Multiplication: " +mNum); 
	} 
	public static void main(String[] args) 
	{ 
		Ex16.multiply(); 
	} 
}
You can also try this code with Online Java Compiler
Run Code

Output

Multiplication: 2000
You can also try this code with Online Java Compiler
Run Code


In this code example, we have used a user-defined function to calculate the multiplication of two numbers. We have created a function called multiply, and then we have called that function by creating an object of that class. The modifier here is used is public to call the user-defined multiply function.

Try this code by yourself on Online Java Compiler.

Must Read Conditional Statements in Java

Frequently Asked Questions

  1. What is the signature of the method in Java?
    A method declaration can also be part of the method declaration in Java. It is the combination of the method name and the parameter list. The name method can be unique or non-unique.
     
  2.  What is the method hiding in Java?
    Method hiding can be defined as any hierarchical structure in Java. A child class, when expressed as static bearing the same signature in the parent class, then the child's method hides the one on the parent class.

Key Takeaways

In this blog, we have covered an introduction to methods in Java. We have also covered the definition of methods along with their syntax. We also talked about types of methods. We also discussed different components required to implement a method in Java. We took several examples of codes with inbuilt and user-defined methods.

If you want to learn and practice more on this topic, please jump to our website, Guided Paths, and for participating in live contests to brush up on your skills Contests.

Live masterclass