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.
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 java.io.PrintSteam. 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.
To get in-Depth knowledge on Java you can enroll for a live demo on Java Online Training
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.
2. Method Declaration in Java
There are 6 components to declare a method in Java.
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.
- Public — In this type, the method is accessible in all classes in the application.
- Protected — Here, the method is accessible in the class in which it is defined and also in its subclasses.
- Private — Method is accessible only in the class it is defined in.
- Default — Method is accessible only within the same class and package in which it is defined.
Take your career to new heights of success with Core Java Training
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.
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.
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
Example
//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
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.