In this tutorial, you will learn about Java methods, how to define methods, and how to use methods in Java programs with the help of examples.
A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++, and Python.
A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design.
Depending on whether a method is defined by the user, or available in the standard library, there are two types of methods in Java:
The Standard library methods are built-in methods in Java that are readily available for use. These standard libraries come along with the Java Class Library that is present in a Java archive (*.jar) file with JVM and JRE.
Some Examples of Standard libraries are :
For Example :
public class CodeMistic
{
public static void main(String[] args)
{
// using the sqrt() method
System.out.print("Square root of 64 is: " + Math.sqrt(64));
}
}
Output :
Square root of 64 is: 8.0
We can also create methods of our own choice to perform some task. Such methods are called user-defined methods.
Here is how we can create a method in Java :
public static void myFunction()
{
System.out.println(“My Function is created.”);
}
Here, we have created a method named myMethod()
. We can see that we have used the public
, static
and void
before the method name.
public
- access modifier. It means the method can be accessed from anywhere. To learn more, visit Java access modifierstatic
- It means that the method can be accessed without any objects. To learn more, visit the Java static Keyword.void
- It means that the method does not return any value. We will learn more about this later in this tutorial.This is a simple example of how we can create a method. However, the complete syntax of a method definition in Java is:
modifier static returnType nameOfMethod (parameter List)
{
// method body
}
The syntax shown above includes :
To call a method in Java, you have to write the method’s name followed by parentheses () and a semicolon ;
Example :
myMethod();
class Main {
public static void main(String[] args) {
System.out.println("About to encounter a method.");
// method call
myMethod();
System.out.println("Method was executed successfully!");
}
// method definition
private static void myMethod(){
System.out.println("Printing from inside myMethod()!");
}
}
Output :
About to encounter a method.
Printing from inside myMethod().
Method was executed successfully!
Parameters are specified after the method name in a class, inside parentheses. You can add as many parameters as you want but just separate them with a comma. Data can be passed to functions as a parameter. Actually, parameters act as variables inside the method.
Example for Passing Parameters :
public class swappingExample
{
public static void main(String[] args)
{
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);
// Invoke the swap method
swapFunction(a, b);
System.out.println("\n**Now, Before and After swapping values will be same here**:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
public static void swapFunction(int a, int b)
{
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}
Output :
Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45
Example: Method Accepting Arguments and Returning Value
public class Main
{
public static void main(String[] args)
{
int result, n;
n = 3;
result = square(n);
System.out.println("Square of 3 is: " + result);
n = 4;
result = square(n);
System.out.println("Square of 4 is: " + result);
}
// method
static int square(int i)
{
return i * i;
}
}
Output :
Squared value of 3 is: 9
Squared value of 4 is: 16