Java Methods or Functions

A Java method is a block of code or collection of statements that performs a particular task and returns a result. A (Void) method in Java can perform a particular task without returning any result.

Methods in Java execute only when they are called by any of the caller methods.

Methods in Java help us implement the DRY (Do Not Repeat Yourself) Principle. The idea behind the principle is that repeating yourself is a bad thing to do when coding, because having the same code in different places makes maintainability harder, once changes in the code will have to happen in many places, instead of one.

Now after reading this you must be thinking that the methods in Java are the same as functions in other languages like C/C++. The answer is pretty much YES. We can say that the ‘Method’ is the object-oriented word for ‘Function’. 

The only difference between Methods and functions is that you can invoke a function anywhere by just mentioning its name with the given arguments.

While talking about the methods then the method is specifically associated with an object, which means you can only invoke a method by mentioning its object before it with a dot(.) notation or operator. Every method must be a part of some class.

Declaration of method:

There are total 6 components in the Java method declaration and they are as follows:

  1. Access-Modifier: it defines how the method will be accessed or from where the method will be accessed. In Java, there are four types of access modifiers and they are as follows:
    Public: Public classes are accessible by all the classes that are present in the application.
    Protected: Protected classes can only be accessed within the class in which they are defined or in their subclasses.
    Private: Private classes can only be accessed within the class in which they are defined.
    Default: Default classes are defined/declared without using any access modifier. It is accessible within the same class in which it is defined.
  2. The return type: The type of data(i.e Data-type) that the method returns after the execution of the code inside it are called as return type of that method. If the method does not return any kind of value then it is called a Void method.
  3. Method Name: The name of the method should be defined according to a task that a particular method is performing.
  4. Parameter list: The list of input parameters preceding their data types, Being separated by a comma and enclosed with parentheses is called a parameter list. If there are no parameters, just use empty parentheses ().
  5. Exception list: The exceptions you expect by the method can throw, you can specify these exception(s).
  6. Method body: The block of code which is enclosed between curly braces, is the body of the method.

Types of Methods in Java

There are two types of methods in Java:

  1. Predefined Method: The methods which are predefined or already defined in the Java class libraries are known as predefined Methods / Built-in Methods. These methods are the same as STL (Standard template library) in the C++ language. We can directly use these methods just by calling them in our program.
  2. User-defined Method: The methods which are written or created by the user or programmer are known as User-defined Methods.

Calling a Method

The method needs to be called for using its functionality. There can be three situations when a method is called: 

A method returns to the code that invoked it when:  

  • It completes all the statements in the method
  • It reaches a return statement
  • Throws an exception

Example 1: Multiplication of two numbers with the help of methods in Java

Code:

Java Code

// Class 1
// Helper class
class Product {

  // Initially taking multiplication as 1
  int mul = 1;

  // Method
  // To find product of two numbers
  public int Multiplication(int a, int b) {

    // Multiplying two integer value
    mul = a * b;

    // Returning product of two values
    return mul;
  }
}

// Class 2
// Helper class
class TUF {

  // Main driver method
  public static void main(String[] args) {

    // Creating object of class 1 inside main() method
    Product pro = new Product();

    // Calling method of above class
    // to multiply two integer
    // using instance created
    int ans = pro.Multiplication(2, 5);

    // Printing the product of two numbers
    System.out.println("Product of given two values is :" +
      ans);
  }
}

Output: Product of given two values is : 10

Example 2: To check if the given number is Even or Odd

Code:

Java Code

// Class 1
// Helper class
class Check {

  // Method
  // To check if given number is even or odd
  public boolean EvenOrNot(int num) {

    // Cheacking if the number is even or odd
    if (num % 2 != 0) {
      return false; // the number is odd
    }

    return true; // the number is even
  }
}

// Class 2
// Helper class
class TUF {

  // Main driver method
  public static void main(String[] args) {

    // Creating object of class 1 inside main() method
    Check chk = new Check();

    // Calling method of above class
    // to check the number is even or odd
    // using instance created
    boolean ans = chk.EvenOrNot(5);

    // Printing the result
    if (ans) {
      System.out.println("The number is Even..!");
    } else {
      System.out.println("The number is odd..!");
    }
  }
}

Output: The number is odd..!

Special thanks to Abhishek Yadav for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article