17java Methods
17java Methods
A Java met hod is a collect ion of st at ement s t hat are grouped t oget her t o perform an operat ion.
When you call t he Syst em.out .print ln met hod, for example, t he syst em act ually execut es several
st at ement s in order t o display a message on t he console.
Now you will learn how t o creat e your own met hods wit h or wit hout ret urn values, invoke a met hod
wit h or wit hout paramet ers, overload met hods using t he same names, and apply met hod abst ract ion
in t he program design.
Here,
Met hod definit ion consist s of a met hod header and a met hod body. The same is shown below:
mo difier: It defines t he access t ype of t he met hod and it is opt ional t o use.
nameOfMetho d: This is t he met hod name. The met hod signat ure consist s of t he met hod
name and t he paramet er list .
Parameter List: The list of paramet ers, it is t he t ype, order, and number of paramet ers of a
met hod. These are opt ional, met hod may cont ain zero paramet ers.
metho d bo dy: The met hod body defines what t he met hod does wit h st at ement s.
Example:
Here is t he source code of t he above defined met hod called max(). This met hod t akes t wo
paramet ers num1 and num2 and ret urns t he maximum bet ween t he t wo:
return min;
}
The process of met hod calling is simple. When a program invokes a met hod, t he program cont rol
get s t ransferred t o t he called met hod. This called met hod t hen ret urns cont rol t o t he caller in t wo
condit ions, when:
The met hods ret urning void is considered as call t o a st at ement . Let s consider an example:
System.out.println("This is tutorialspoint.com!");
The met hod ret urning value can be underst ood by t he following example:
Example:
Following is t he example t o demonst rat e how t o define a met hod and how t o call it :
return min;
}
}
Minimum value = 6
Example:
public class ExampleVoid {
Rank:A1
Passing Paramet ers by Value means calling a met hod wit h a paramet er. Through t his t he argument
value is passed t o t he paramet er.
Example:
The following program shows an example of passing paramet er by value. The values of t he
argument s remains t he same even aft er t he met hod invocat ion.
Let s consider t he example shown before for finding minimum numbers of int eger t ype. If, let s say we
want t o find minimum number of double t ype. Then t he concept of Overloading will be int roduced t o
creat e t wo or more met hods wit h t he same name but different paramet ers.
// 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;
}
}
Minimum Value = 6
Minimum Value = 7.3
Overloading met hods makes program readable. Here, t wo met hods are given same name but wit h
different paramet ers. The minimum number from int eger and double t ypes is t he result .
Example:
The following program displays all of t he command-line argument s t hat it is called wit h:
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100
Typically, you will use a const ruct or t o give init ial values t o t he inst ance variables defined by t he
class, or t o perform any ot her st art up procedures required t o creat e a fully formed object .
All classes have const ruct ors, whet her you define one or not , because Java aut omat ically provides a
default const ruct or t hat init ializes all member variables t o zero. However, once you define your own
const ruct or, t he default const ruct or is no longer used.
Example:
Here is a simple example t hat uses a const ruct or:
// A simple constructor.
class MyClass {
int x;
Example:
Here is a simple example t hat uses a const ruct or:
// A simple constructor.
class MyClass {
int x;
10 20
typeName... parameterName
In t he met hod declarat ion, you specify t he t ype followed by an ellipsis (...) Only one variable-lengt h
paramet er may be specified in a met hod, and t his paramet er must be t he last paramet er. Any
regular paramet ers must precede it .
Example:
public class VarargsDemo {
For example, you might use finalize( ) t o make sure t hat an open file owned by t hat object is closed.
To add a finalizer t o a class, you simply define t he finalize( ) met hod. The Java runt ime calls t hat
met hod whenever it is about t o recycle an object of t hat class.
Inside t he finalize( ) met hod, you will specify t hose act ions t hat must be performed before an object
is dest royed.
Here, t he keyword prot ect ed is a specifier t hat prevent s access t o finalize( ) by code defined
out side it s class.
This means t hat you cannot know when or even if finalize( ) will be execut ed. For example, if your
program ends before garbage collect ion occurs, finalize( ) will not execut e.