In this tutorial, we will uncover a question that is mostly asked in the programming world i.e What are methods in Java? We will learn what they are, how to use them, and when to use them in a Java program.
What are Methods in Java?
A method in Java is a block that generally contains a set of statements to complete a task or an operation.
Let us try to understand Methods in a simpler way, consider a scenario where you have a common task that is repeatedly performed in a program. There are two ways to handle this scenario while programming.
- You can write the same common program logic again and again and complete the program.
- You can separate the common logic and write it in a Method once and just call that method again and again.
Using a method to perform a repeating task is a very time-consuming process and it also decreases the lines of code of a program.
What are the components of a method?
A method has mainly 5 components. These components are required to declare a method in a Java program. They are:
- Access Specifier
- Return Data Type
- Name of the Method
- Arguments
- Body of the Method
Access Specifier in Java
An Access Specifier specifies where this method can be accessed. It generally defines the scope of the method in a Java program. In Java, there are 4 types of access specifiers, they are:
public
The method that has a public access specifier can be accessed from any class or any part of the program.
private
A method that has a private access specifier can only be accessed within the class where it is defined.
protected
A method that has a protected access specifier can be accessed within the same package. It can also be accessed from other packages if they inherit the class where this method exists.
default
A method that does not specify any access specifier will have a default access specifier assigned that is the default access specifier. Any method that has this specifier can only be accessed within the same package.
Return Data Type in Java
A method has a return type or return data type that tells the Java compiler about what type of data this method will return after it is executed. If a method has nothing to return then the return type of that method is ‘void’.
For example, if there is a method whose name is ‘calculateAverage’ whose return type is ‘float’. Then we can easily say that this method will (by the name) calculate the average of some numbers and finally return the output in the form of a float value.
Name of the Method in Java
The name for a method is used to call a method so that it can be executed to perform a task. The name was given to a method that generally relates to what the method is supposed to do so that it is easy to remember and understand what the method will do when it is called.
Arguments in Java
Arguments are the inputs given to a method. A method may or may not have arguments depending on the implementation. If a method has nothing to take as an input then it doesn’t need to have any arguments. But if it requires some input, then it can be mentioned in the method signature. After that whenever we want to call that method, we need to pass those arguments, or else we will not be able to call that method.
Body of the Method in Java
The body of the method is the actual block where all the statements of a method reside. The statements or programming logic written in this part of the method is executed where the method is called.
Basic example of a method:
public int addTwoNumbers(int num1, int num2) { int sum = num1 + num2; return sum; }
The example above shows that:
- The method has ‘public’ access
- The return type is given as ‘int’
- The name of the method is ‘addTwoNumbers’
- Arguments/inputs taken by the method are num1 and num2 which are of type ‘int’
- The body contains the programming logic for adding the two numbers which are given as input to the method and return their sum which is of type ‘int’
The way mentioned above is a simple way of writing a simple method in Java. This way you can add more and more logic within a method body to create a complex method that can save you a lot of time.
But there is not always a need to write a method for each and everything. Java provides some in-built methods that are pre-defined for the developer to use. This brings us to the types of method in Java.
What are the types of Methods in Java?
There are two types of methods in Java. They are:
- Predefined Methods
- User-defined Methods
Predefined Methods in Java
Predefined methods are those methods that are already defined in the Java compiler. These methods are pre-tested and can be used in any program. Java has many predefined methods.
User-defined Methods in Java
User-defined methods are those methods that are defined by the developer so that they can be used later in a program. The developer has to test them for the correctness of output and maintain them.
Let us see how we can create and use a method in Java:
class Program { public static void main (String agmt[]) { Program program = new Program(); //Calling printSum method to print the sum of 5 and 24 program.printSum(5,24); //Calling printSum method to print the sum of 35 and 64 program.printSum(35,64); } //Defining method printSum which takes two numbers and prints their sum void printSum(int number1, int number2) { int sum = number1 + number2; //Calling pre-defined method println() to print to the console System.out.println("Sum: " + sum); } }
Output:
Sum: 29 Sum: 99 |
The above example shows how the printSum() method is called twice to calculate and print the sum of number1 and number2.