In this article, you’ll learn about Java methods, how to define a Java method and use them in your program with the help of examples.
Methods
A Java method is a collection of statements that are grouped together to perform an operation.
Methods allow us to reuse the code without retyping the code i.e. you can write a method once, and use it multiple times. It also makes code more readable and easier to debug.
As Java is an Object Oriented Programming language, every method must be a part of some class which defines the behavior of a class.
Types of Methods
In Java, depending on whether a method is defined by the user or available in the standard library, methods are of two types:
- Standard Library Methods
- User-defined Methods
Standard Library Methods
The Standard library methods are built-in methods in Java that are ready to use.
Examples of Standard library methods are:
- print(): method comes under java.io.PrintSteam , prints the string written within the quotation.
- sqrt(): method of Math class that returns the square root of a specific number.
For example:
public class Number {
public static void main(String[] args) {
//max() of Math class will print maximum number out of 16,8
System.out.print("Maximum number is: " + Math.max(16,8));
}
Output:
Maximum number is: 16
User-defined Methods
You can also define your own methods inside a class and such methods are called user-defined methods.
To work with user-defined methods, firstly we have to define a method then call it or use it.
Defining a method
Before you can use a method (call a method), you need to define it.
Syntax:
The syntax of defining a method:
modifier returnType nameOfMethod(Parameter list) {
//Method body
}
More generally, method declarations have the following components, in order:
modifier-: defines access type of the method i.e. whether this method will be called or will be visible to any other class or package or not. In Java, access modifiers are of four types:
- public: accessible within the class, within the package and outside the package.
- protected: accessible within the class, within the package in which this class is defined and also outside the package but through inheritance only.
- private: accessible only within the class in which it is defined.
- default (defined without using any modifier): accessible within the class and within the package only.
returnType: defines the data type of the value returned by the method or void if a method does not return a value.
nameOfMethod: You can give any name to a method. But it’s more conventional to name it upon the task it performs. As per the naming convention, a method should start with lower case and if it contains more than one word then every word starting should be of upper case.For example: calculateAddition, displayStudentDetails etc.
Parameter list: Parameters are the values passed to a method. You can pass any no of arguments to a method. If there are no parameters, you must use empty parentheses ().
Method body : The code inside curly braces { } is the body of the method. It defines what the method actually does, how the parameters are manipulated with statements and what values are returned.
Example:
public int max(int x, int y) { if (x > y) return x; else return y; }
Calling a method
Now we have defined a method, we need to use it. For that, we have to call the method to use its functionality.
Syntax:
Syntax of calling a method:
nameOfMethod(Parameter list);
When calling a method it consists of the method name and a parameter list (number of parameters, type of the parameters and order of the parameters).
The way of calling the max() method which we have defined above:
max(5, 10);
Example:
public class Number { public static void main(String[] args) { //calling max() method int result = max(8, 7); System.out.print("Greater no: " + result) } //defining max() method public static int max(int x, int y) { if (x > y) return x; else return y; } }
Output:
Greater no: 8
Example: Java Methods
class Addition { int result; //addTwoNumbers() definition //method with two parameters and //returning integer value.therefore,returnType is int public int addTwoNumbers(int a, int b) { // adding two values. result = a + b; //calling display() method within the same class this.display(); return result; } //display() definition //method with no parameter and //returning nothing.therefore,returnType is void public void display() { System.out.println("display() in Addition class calling inside addTwoNumbers()"); } } public class ExampleDemo { public static void main(String[] args) { // creating an instance of Addition class Addition addition = new Addition(); // calling addTwoNumbers() method to add two values // using instance created in above step. int result = addition.addTwoNumbers(10, 5); System.out.println("Addition of two Numbers :" + result); } }
Output:
display() in Addition class calling inside addTwoNumbers() Addition of two Numbers :15
Control flow of above program: