Method
is a sub block of a class that is used for implementing the logic of an object’s
operation. Logic must be placed only inside a method, not directly at class
level. If we place logic at class level compiler throws error. Hence at class
level we are allowed to place only variable and method creation statement.
Method Terminology
1. Method prototype : The head
portion of the method is called method prototype.
static void m1( )
2. The “{
}” region is called method body, and the statements placed inside method body
is called logic.
static void
m1(int a,int b )
{
int
c = a+b;
System.out.println(“result”+c);
}
3.
Method parameters and arguments – The variables declared in method parenthesis “(
)” are called parameters. We can define method with 0 to ‘n’ number of
parameters. The input values passed to parameters are called arguments. In
method invocation we must pass arguments according to methods parameters order
and type.
4. Method
Signature - The combination of [Method name + parameter list] is called Method
signature.
5.
Method returns type – The data type keyword that is placed before method name
is called return type. It tells to compiler, JVM and developer, about the type
of the value which is returned from this method after its execution. If we don’t
want to return a value for a method, we must use void keyword as return type
for that method.
Types of methods –
Basically
concrete methods are divided into 3 types –
1.
Based on static modifier we have two types of methods-
·
Static methods
·
Non-static methods
2.
Based on return type we have two types of methods –
·
Void methods
·
Non-void return type
3.
Based on parameter we have two types of methods –
·
Parameterized method
·
Non-parameterized method
Static method – A method
which has static keyword in its definition is called static method. Example –
static void
m1( )
{
System.out.println(“Hello I am in m1( )”);
}
JVM
doesn’t execute static method by itself like static variables. They are
executed only if they are called explicitly by developer either from main
methods, or from static variable as its assignment statement or from static
block. They are executed in the order they are called, not in the order they
are defined. All the static methods logic is stored in method area and that
logic is executed in java stacks area in main thread, by creating separate
stack frame.
Example –
public class
Method1 {
static void
m1()
{
System.out.println("m1( ) is atarted");
}
static void
m2()
{
System.out.println("m2( ) is started");
int m=20;
int n=30;
System.out.println("m2( ) end");
}
static void
m3()
{
System.out.println("m3( ) is started");
int m=20;
int n=30;
System.out.println("m3( ) end");
}
public static void
main(String[] args) {
System.out.println("main( ) started");
m1();
m2();
int m=40;
int n=50;
System.out.println("main( ) ended");
}
}
Output –
main( ) started
m1( ) is atarted
m2( ) is started
m2( ) end
main( ) ended
Non-static methods – The method
which does not have static keyword in its prototype is called non-static
method. Like non-static variable, non-static methods should also be called only
by using object. JVM doesn’t execute non-static method automatically. When
object is created, we should call them explicitly using object. If we call them
directly from main() method then it leads to CE:”non-static method cannot be
referenced from static context”.
Example –
public class
Method1 {
void
m1()
{
System.out.println("in m1 method");
}
public static void
main(String[] args) {
System.out.println("in main method");
// CE: cannot make a static reference to the
non-static method m1()
m1();
}
}
Example –
public class
Method1 {
void
m1()
{
System.out.println("in m1 method");
}
public static void
main(String[] args) {
System.out.println("in main method");
Method1 m = new
Method1();
m.m1();
}
}
Output –
in main method
in m1 method
Void and non-void methods – If the
method return type is void, it is called as void method, else it is called non
void method. Non-void method must return a value after its execution, also that
value type must be compatible with method return type and it must be less than
or equal to methods return type, else it leads to compile time error.
Example –
public class
Method2 {
public void
display()
{
System.out.println("Hello java");
}
public static void
main(String[] args) {
Method2 m1 = new
Method2();
m1.display();
}
}
Output –
Hello java
Example –
public class
Method2 {
public double
display()
{
int x=10;
return x;
}
public static void
main(String[] args) {
Method2 m1 = new
Method2();
System.out.println(m1.display());
}
}
Output –
10.0
Parameterized and non parameterized methods
– If
a method is created with parameters, it is called parameterized method, else it
is called as non-parameterized method
Example –
public class
Method2 {
static void
m1()
{
System.out.println("Hello i am a non-parameterized method");
}
static void
m2(int x)
{
System.out.println("Hello i am a parameterized method "+x);
}
public static void
main(String[] args) {
m1();
m2(5);
}
}
Output –
Hello i am a non-parameterized method
Hello i am a parameterized
method 5
No comments:
Post a Comment