1 Java Methods
1 Java Methods
MIDTERM MODULE
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.
Example
Here is the source code of the above defined method called min(). This method takes
two parameters num1 and num2 and returns the maximum between the two −
/** the snippet returns the minimum between two numbers */
else
min = n1;
return min;
}
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 −
Following is the example to demonstrate how to define a method and how to call it −
Example
public class ExampleMinNumber {
return min;
}
}
CC 106
APPLICATION DEVELOPMENT AND EMERGING TECHNOLOGY
JAVA METHODS
MIDTERM MODULE
CC 106
APPLICATION DEVELOPMENT AND EMERGING TECHNOLOGY
JAVA METHODS
MIDTERM MODULE
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b =
" + b);
}
}
Method Overloading
When a class has two or more methods by the same name but different parameters, it
is known as method overloading. It is different from overriding. In overriding, a method
has the same method name, type, number of parameters, etc.
Let’s consider the example discussed earlier for finding minimum numbers of integer
type. If, let’s say we want to find the minimum number of double type. Then the
concept of overloading will be introduced to create two or more methods with the same
name but different parameters.
The following example explains the same −
Example
public class ExampleOverloading {
// for integer
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
CC 106
APPLICATION DEVELOPMENT AND EMERGING TECHNOLOGY
JAVA METHODS
MIDTERM MODULE
else
min = n1;
return min;
}
// for double
public static double minFunction(double n1, double n2) {
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100
Student(int age) {
this.age = age;
}
}
Example
Here is an example that uses this keyword to access the members of a class. Copy
and paste the following program in a file with the name, This_Example.java.
public class This_Example {
// Instance variable num
int num = 10;
This_Example() {
System.out.println("This is an example program on keyword
this");
}
CC 106
APPLICATION DEVELOPMENT AND EMERGING TECHNOLOGY
JAVA METHODS
MIDTERM MODULE
This_Example(int num) {
// Invoking the default constructor
this();
Variable Arguments(var-args)
CC 106
APPLICATION DEVELOPMENT AND EMERGING TECHNOLOGY
JAVA METHODS
MIDTERM MODULE
JDK 1.5 enables you to pass a variable number of arguments of the same type to a
method. The parameter in the method is declared as follows −
typeName... parameterName
In the method declaration, you specify the type followed by an ellipsis (...). Only one
variable-length parameter may be specified in a method, and this parameter must be
the last parameter. Any regular parameters must precede it.
Example
public class VarargsDemo {
This means that you cannot know when or even if finalize( ) will be executed. For
example, if your program ends before garbage collection occurs, finalize( ) will not
execute.
REFERENCES
Java Methods retrieved from https://www.tutorialspoint.com/java/java_methods.htm in October
2020
CC 106
APPLICATION DEVELOPMENT AND EMERGING TECHNOLOGY