Difference between Function and Method in Java
In this tutorial, we are going to learn about the differences between function and method in Java.
What is Method in Java?
In Java, a method is a block of code that performs a specific task and can be executed by calling its name. It is a fundamental building block of object-oriented programming and helps in organizing and structuring code into reusable components. Methods are declared within classes and can have a return type, parameters, and body. The return type specifies the type of value that the method will return after its execution. If a method does not return any value, its return type is specified as void.
What is Function in Java?
In Java, the term "function" is a block of code that performs a specific task. However, in a strict sense, the term "function" is commonly associated with programming languages that support functional programming paradigms, where functions are treated as first-class citizens and can be passed as arguments, returned as values, and stored in variables.
Sr No. | Parameters | Function | Method |
1 | Scope of Execution | Functions have a wider scope of execution as they can be called from anywhere in the program, even without the need for an object instance. | Methods are associated with objects or classes and can only be invoked on specific instances or the class itself. |
2 | Invocation | Functions are typically invoked using the class name, as they are implemented as static methods. For example, Math.max(a, b) is a function call. | Methods are invoked on objects using the dot notation. For example, myObject.doSomething() is a method call. |
3 | Object Dependency | Functions do not depend on any particular object or instance. They are self-contained and operate solely on the provided parameters. | Methods, however, are associated with a specific object or class and can access and modify the object's state. |
4 | Static vs. Non-static | Functions are implemented as static methods, meaning they belong to a class rather than an instance. They can be called directly without creating an object. | Methods can be static or non-static. Non-static methods are tied to object instances and require an object to be created before they can be invoked. |
5 | Inheritance and Polymorphism | Functions are not inherited or overridden since they are not associated with classes. | Methods can be inherited from parent classes and overridden in child classes to provide specific implementations. |
Example 1
In the above example, we have a class called MethodFunctionDemo that contains three members. The add function is a static method that takes two integer parameters (a and b) and returns their sum. The printWelcomeMessage method is a non-static method that doesn't take any parameters. It simply prints a welcome message to the console. The factorial method is a non-static method that calculates the factorial of a number n using recursion. It returns the factorial value.
FileName: MethodFunctionDemo.java
public class MethodFunctionDemo { // Function: Adds two numbers and returns the sum of that two numbers public static int add(int a, int b) { return a + b; } // Method: Prints a welcome message public void printWelcomeMessage() { System.out.println("Welcome to the JavaTpoint!"); } // Method: Computes the factorial of a number recursively public int factorial(int n) { if (n == 0 || n == 1) { return 1; } else { return n * factorial(n - 1); } } public static void main(String[] args) { MethodFunctionDemo demo = new MethodFunctionDemo(); // Function call int sum = add(5, 3); System.out.println("Sum: " + sum); // Method call demo.printWelcomeMessage(); // Method call int factorialResult = demo.factorial(5); System.out.println("Factorial: " + factorialResult); } }
Output
Sum: 8 Welcome to the JavaTpoint! Factorial: 120
Example 2
In the above example, The findMax function is a static method that takes an integer array (arr) as a parameter. It iterates over the elements of the array and finds the maximum element. The maximum value is then returned and the printArray method is a non-static method that takes an integer array (arr) as a parameter. It loops through the elements of the array and prints each element to the console.
FileName: MethodFunctionExample.java
public class MethodFunctionExample { // Function: It Finds the maximum element in the array public static int findMax(int[] arr) { int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } // Method: Prints all the elements inside the array public void printArray(int[] arr) { for (int element : arr) { System.out.print(element + " "); } System.out.println(); } public static void main(String[] args) { int[] numbers = { 5, 8, 2, 10, 3 }; // Function call int maximum = findMax(numbers); System.out.println("Maximum element: " + maximum); MethodFunctionExample example = new MethodFunctionExample(); // Method call example.printArray(numbers); } }
Output
Maximum element: 10 5 8 2 10 3