Methods in Java
A method is a block of code which is separated from the main method and performs specific tasks or operations. They are defined within classes and can be called (invoked) to execute their code. Methods are used to encapsulate functionality and promote code reusability.
Syntax
- returnType - Specifies the type of value the method will return.
- methodName - The name of the method, which must be a valid identifier.
- parameterTypeX - The data type of each parameter.
- parameterNameX - The name of each parameter.
Note: If we do not need parameters, it is not necessary to write anything inside parentheses.
Method with return type void
This hello() method will print "Hello World!". We can call methods multiple times.
Execution of our code starts from Main method. Everything inside main method will be executed by the compiler. If we write something outside the main method, like a function. It will not be executed until we call it in our main method.
Method with return type int
Parameterized Methods
Till now, we have studied about unparameterized methods. Now we will discuss about parameterized methods.
Parameterized methods are the methods which take variables as parameters and perform operations on them. These methods are designed to take input values, process them, and possibly return a result or perform some action based on the provided parameters.
Note: The return value of a method can be stored in a variable.
Parameters and Arguments
When there is a variable in the main function and we need it in the other function then we pass it to other function when we call it(inside parentheses). We can pass as many arguments as we want.
Parameters: variables in parenthesis when we declare function.
Arguments: variables in parenthesis when we call function.
In this example, we are calling the same function with different parameter values. The method printName will accept the parameters which we provide as arguments and use this value to execute the code inside the function.
Note: We will pass as many arguments as we want. The number of arguments and parameters must be same.
Method Overloading
Method overloading allows you to define multiple methods in the same class with the same name but;
- number of parameters will be different, Or
- if same, their data types will be different
This add method is overloaded as number of parameters is different.
Method Overloading with different data types of Parameters
This add method is overloaded as parameters have different data types.
Recursion
Recursion means calling the method within the same method. This is done to break the one complicated problem into many simpler problems and then solving the simpler problems.
Let us consider an array of ten numbers and we want to sort it inascending order. It will be difficult. If we split our array into pairs of two and then try to sort it, it will be easy.
In this Example;
- The factorial function calculates the factorial of n recursively.
- The base case is when n is 0 or 1; in this case, the function returns 1.
- In the recursive case, the function calls itself with n-1, which is a smaller problem, until it reaches the base case.
- The result is calculated as n * factorial(n - 1).
When you run this program with n = 5, it calculates 5! by breaking it down into smaller multiplications and returns the result of 120. This is a simple example of how recursion works in Java to solve a problem by breaking it down into smaller, similar sub-problems.
Static VS Public Methods
The difference between static and public methods is that static methods can be called without the creation of an object while public methods can only be accessed using object of the class.
As the concept of static and public method invloves object, you will have to learn about Object Oriented Programming.
Static Method
Public Method
Practice Exercises
Complete these exercises to reinforce your learning and earn XP