Java Method – Declaring and Calling Method with Example

Java methods are the statements gathered together to perform a specific task. When you call any pre-defined function like sqrt(), a series of codes related to sqrt() run in the background which is already stored in the library. In other words, the Java method is a collection of statements or commands which are used to run a particular task. This article specially designed for you to learn how to declare and call a Java method with an example.

Keeping you updated with latest technology trends, Join DataFlair on Telegram

1. Types of Java Methods

Java Methods can be categorized into two types

  • Standard Library Methods
  • User-defined Methods

1.1 Standard Library

Standard Library Methods in Java are built-in methods, they are always available to use. These libraries come along with the Java Class Library (JCL) in a .jar file with Java Virtual Machine and Java Runtime Environment.

Some of the examples of Standard libraries are:

  • print()

This method comes under It prints the string which is written inside print() method in the quotation marks

  • sqrt()

This method comes under Math class. It returns the square root of a number.

1.2 User-defined

Java Method is a block of statements in or commands which are used to run a particular task and return the result or value to the caller, it can also perform a task without returning any value. They also provide the user to use a particular code again without retyping. A Java method must always be a part of a class in Java, which is unlike C, C++, and Python.

Get 240+ Free Python Tutorials Series from Beginners to Professionals

2. Method Declaration in Java

There are 6 components to declare a method in Java.

Java Method Tutorial

2.1 Access Specifier

An access specifier in Java defines the access type of the method, i.e. it defines in which part of the program it can be accessed.

Java has 4 types of access modifiers

  1. Public – In this type, the method is accessible in all classes in the application.
  2. Protected – Here, the method is accessible in the class in which it is defined and also in its subclasses.
  3. Private – Method is accessible only in the class it is defined in.
  4. Default – Method is accessible only within the same class and package in which it is defined.

2.2 Return Type

The return type defines the type of data that is returned by the method. It can return any kind of value like integer, float, double, etc and also it returns void when nothing is returned by the method.

2.3 Method Name

A method name is used to define the name of a method, it is the same as the naming of field types with a little difference in the convention. A method is invoked by its name.

2.4 Parameter list

The parameter list is a list with input parameters that are separated by commas, they are preceded by their data types within closed parenthesis, and also, if there are no parameters then there is an empty parenthesis ().

2.5 Method Body

The body of the Java method must be enclosed within braces, all the codes for the method are written in it. All the operations are performed inside a Java method.

Java Method Body in Java

2.6 Method Signature

A method signature is consists of the method name and parameter list in Java i.e. number, type, and order of parameters. Java return type and exceptions are not a part of the method signature.

Let’s revise the concept of Data Types in Java

3. How to Call a Method in Java?

In Java to use a method, we need to call it. A method is called according to its functionality. The method either returns a value or return nothing(no return value). The process of calling a method is very simple. When the program invokes any method, the control automatically gets transferred to the calling method. It is called in any of these 3 situations

  • When it completes all the statements
  • An exception is thrown to it
  • When it reaches a return statement

Do you know Static vs Instance Methods in Java


//Program to illustrate methods in Java
package com.dataflair.method;
class Addition
  int sum = 0;
  public int additionFunction(int operand1, int operand2)
    // adding integer values
    sum = operand1 + operand2;
    return sum;
class AdditionProgram{
    public static void main(String[] args){
      // creating an instance of AdditionProgram class
      Addition additionObj = new Addition();
      // calling additionFunction() method to add two integer using instance created in above step.
      int sumValue = additionObj.additionFunction(10,20);
      System.out.println("Sum of two integer values is : "+ sumValue);

Output-Addition Program in Java Method

4. Summary

With the help of methods, you can perform any specific task. They allow us to reuse the code without typing again and again. Java method makes code readable and easier to debug.

It’s time to uncover the difference between Overloading and Overriding Methods in Java

Hope you enjoyed the article. Please share your suggestions and feedback in the comment section!

6 Responses

  1. natalia says:

    Simply wɑnna input on few gеneral things, The website layout is perfect, articles are fantastic

  2. Mohammed Tasi'u says:

    Hello! I created a window using java, and I want to put color to the background and I don’t know how to do so. Help!

  3. Madhu says:

    Excellent work sir…

  4. Tanisha says:

    this is seriously helpful

  5. mack says:

    Im confused. I don’t see where they actually explained the actual process of how to call a method…

  6. mack says:

    So this is it here right? additionObj.additionFunction the additionObj. is calling the additionFunction method right?

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.