Method in Java
In Java, a method is like a function which is used to expose the behavior of an
object.
Advantage of Method
o Code Reusability
o Code Optimization
A Java method is a collection of statements that are grouped together to
perform an operation. When you call the [Link]() 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
Creating Method
Considering the following example to explain the syntax of a method −
Syntax
public static int methodName(int a, int b) {
// body
Here,
public static − modifier
int − return type
methodName − name of the method
a, b − formal parameters
int a, int b − list of parameters
Method definition consists of a method header and a method body. The same
is shown in the following syntax −
Syntax
modifier returnType nameOfMethod (Parameter List)
// method body
The syntax shown above includes −
modifier − It defines the access type of the method and it is optional to use.
returnType − Method may return a value.
nameOfMethod − This is the method name. The method signature consists
of the method name and the parameter list.
Parameter List − The list of parameters, it is the type, order, and number of
parameters of a method. These are optional, method may contain zero
parameters.
method body − The method body defines what the method does with the
statements.
Method Calling
For using a method, it should be called. There are two ways in which a method is
called i.e., method returns a value or returning nothing (no return value).
The process of method calling is simple. When a program invokes a method, the
program control gets transferred to the called method. This called method then
returns control to the caller in two conditions, when −
the return statement is executed.
it reaches the method ending closing brace.
Example
public class ExampleMinNumber {
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
[Link]("Minimum Value = " + c);
}
/** returns the minimum of two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
This will produce the following result −
Output
Minimum value = 6
The void Keyword
The void keyword allows us to create methods which do not return a value. Here,
in the following example we're considering a void method methodRankPoints.
This method is a void method, which does not return any value.
Example
public class ExampleVoid {
public static void main(String[] args) {
methodRankPoints(255.7);
}
public static void methodRankPoints(double points) {
if (points >= 202.5) {
[Link]("Rank:A1");
}else if (points >= 122.4) {
[Link]("Rank:A2");
}else {
[Link]("Rank:A3");
}
}
}
This will produce the following result −
Output
Rank:A1
class Shape {
public void display() {
[Link]("Inside display");
class Rectangle extends Shape {
public void area() {
[Link]("Inside area");
class Cube extends Rectangle {
public void volume() {
[Link]("Inside volume");
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
[Link]();
[Link]();
[Link]();
Output
Inside display
Inside area
Inside volume