MY mENU


Friday 25 January 2013

Methods in Java


Method: method is a block of a class that contains logic of that class. Logic must be placed only inside a method, not directly at class level. If we place logic at class level compiler throws error.

Method Prototype: the head portion of the method is called method prototype.

Method body and logic: the “{}” region is called method body, and the statements placed inside method body is called logic.

Method parameters and arguments: the variables declared in method parenthesis “( )” are called parameters. We can define method with ZERO to ‘n’ number of parameters.
The values passing to those parameters are called arguments. In method invocation we must pass arguments according to method parameters order and type.

Ex: void add (int a, float b) {  }     //here int a, float b are method parameters
Add (50, 60.5);   // here 50, 60.5 are arguments.

Method Signature: the combination of [Method name+parameters list] is called method signature.

Ex: void add (int a, int b) { }   // here add (int a, int b) is the method signature.

Method Return type: the keyword that is placed before method name is called method return type. It tells to compiler and JVM about the type of the value is returned from this method after its execution.

Void return type keyword: if we do not want to return any value a method, we must use “void” as return type. It tells that the method does not return any value. If we want to return any value a method, we must place data type keyword as return type.

Main method terminology with all above parts:

Public static void main (String[] args)

Public -- Accessibility modifier
Static----Modifier
Void----Return Type
Main----Method Name
String [ ]----Parameter Type
Args------Parameter Name

The process of creating method with body is called method definition. This method is called Concrete Method.

Ex: public static void add(int a, int b){
System.out.println(a+b);
}

The process of creating method without body is called declaring a method/ method declaration. This method is called also abstract method. In method declaration the modifier “abstract” is mandatory and also should be terminated with “;”.

Ex: public native abstract void add(int a, int b);

  •  When we call a method control send to that method.
  •  If we pass argument that value is stored in parameter variable.
  • After method execution that parameter variable is destroyed and control is sent back to calling method.
  •   Control is sent back to calling method with value if method has return type is not void.


    Types of Methods: Basically concrete methods are divided into 3 types:
       Based on static modifier we have two types of methods
  •  Static methods
  •  Non-static methods

We can call static methods directly from main method, but we cannot call non-static methods directly from main method. It leads CE:”non-static method cannot be referenced from static context”, because class level members will not get memory directly. JVM provides memory only if we use either “static or new” keywords.

Main method has static keyword in its prototype since it is the initial point of class login execution; it must be identified and gets memory directly by JVM. Hence it has static keyword in its definition. Of course it must be called without object creation.

class MethodStatic
{          static void m1(){
                                System.out.println("in M1()");
                }
                void m2(){
                                System.out.println("M2() method");
                }
                public static void main(String[] args)
                {
                                System.out.println("Hello World! this is main Method");
                                m1();
                                MethodStatic ms=new MethodStatic();
                                ms.m2(); //it gets memory with reference to “ms” variable.
                }  }

2.  Based on return type we have two types of methods
  •  Void methods
  •  Non-void methods

In void methods statements are optional, but in non-void methods return statement is mandatory with value range less than or equals to method return type range.
If we do not place return statement in non-void methods compiler throws CE: “missing return statement”

Types of return statements:  return is only allowed in void method and constructor, and it is optional.

 Return is only allowed in non-void methods, and it is mandatory.

Basically return statement is used to terminate method execution and for sending control back to calling method.

In general, void methods are called in three ways 1. Directly—m1(); 2. As variable initialization statement—int x=m1();; 3.As SOPLN() argument—Sopln(m1());

The non-void methods can be called in all three ways.  In the first way the return value is lost. In the second way the returned value is stored in the destination variable. In the third way the returned value is printed on console.

3Based on parameter we have two types of methods
  •          Parameterized methods
  •          Non-parameterized methods