Java - Methods
Java - Methods
Java Methods
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.
In this tutorial, we 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.
modifier − It defines the access type of the method and it is optional to use.
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.
return min;
}
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 methods returning void is considered as call to a statement. Lets consider an example −
System.out.println("This is tutorialspoint.com!");
Following is the example to demonstrate how to define a method and how to call it −
return min;
}
}
Output
Minimum value = 6
Output
Rank:A1
Passing Parameters by Value means calling a method with a parameter. Through this, the
argument value is passed to the parameter.
The following program shows an example of passing parameter by value. The values of the
arguments remains the same even after the method invocation.
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = "
}
}
Output
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.
// for integer
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
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;
}
}
Output
Minimum Value = 6
Minimum Value = 7.3
Overloading methods makes program readable. Here, two methods are given by the same name
but with different parameters. The minimum number from integer and double types is the result.
A command-line argument is the information that directly follows the program's name on the
command line when it is executed. To access the command-line arguments inside a Java
program is quite easy. They are stored as strings in the String array passed to main( ).
Example
The following program displays all of the command-line arguments that it is called with −
Output
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100
Note − The keyword this is used only within instance methods or constructors
In general, the keyword this is used to −
Differentiate the instance variables from local variables if they have same names, within
a constructor or a method.
class Student {
int age;
Student(int age) {
this.age = age;
}
}
Call one type of constructor (parametrized constructor or default) from other in a class. It
is known as explicit constructor invocation.
class Student {
int age
Student() {
this(20);
}
Student(int age) {
this.age = age;
}
}
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.
This_Example() {
System.out.println("This is an example program on keyword this")
}
This_Example(int num) {
// Invoking the default constructor
this();
Output
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.
Output
For example, you might use finalize( ) to make sure that an open file owned by that object is
closed.
To add a finalizer to a class, you simply define the finalize( ) method. The Java runtime calls that
method whenever it is about to recycle an object of that class.
Inside the finalize( ) method, you will specify those actions that must be performed before an
object is destroyed.
Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined
outside its class.
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.