Friday, 31 May 2013

Methods in JAVA

A Java method is a collection of statements that are grouped together to perform an operation.

Declaration of method:

modifier returnValueType methodName(list of parameters) {
  // Method body;
}

More generally, method declarations have following components, in order:

Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.

The access to classes, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes.There are four access modifiers used in java. They are publicprivateprotectedno modifer (declaring without an access modifer).

Public: It CAN be accessed from ANYWHERE.
Private: It CAN be accessed from SameClass.
Protected: It CAN be accessed from Sameclass in same package and subclass with in other package.
noModifier: It CAN be accessed from class in same package.

For complete reference of access modifiers, please look into "Access Modifiers in JAVA" post.

Return type: A method may return a value. The returnValueType is the data type of the value the method returns, or void if the method does not return a value.

Method name: This is the actual name of the method.The rules for field names apply to method names as well, but the convention is a little different. We can not use Java keywords as method names.

Parameters: A comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.

Method body: The method body contains a collection of statements(including the declaration of local variables)  enclosed between braces that define what the method does.

Example: Let us consider main() method.

Public static void main(String []args){
//write statements here 
}

Public and static are modifies.
void is return type which specifies it does not return any value. So it is void.
main is name of the method.
args[] is parameter to the method.
code exists between {} is method body.

Calling a Method:

In creating a method, you give a definition of what the method is to do. To use a method, you have to call or invoke it. There are two ways to call a method; the choice is based on whether the method returns a value or not.

When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed or when its method-ending closing brace is reached.

Example:
***********************************************************
public class MethodExample {

/**
 * This is static method. We can acceee this method using classname. 
  * and as this is public method we can access this method from out side 
  * the class also.
 */
public static void getName() {
System.out.println("calling getname() method");
}

/**
 * This is instance method.We can access this method using instance of 
  * class and as this is public method we can access this method from 
  * out side the class also.
 * 
 * @param gs
 * @param ns
 * @return
 */
public float getSalary(float gs, float ns) {
// here ts is local variable to this method. we can access ts with in this
// method only
float ts = gs + ns;
// return type of method is float so we should return float value.
return ts;
}

/**
 * This is also instance method but it is private method. so we can not 
  * access this method from out side the class
 */
private void privateMethod() {
System.out.println("calling privateMethod()");
}

/**
 * @param args
 */
public static void main(String[] args) {

MethodExample example = new MethodExample();

/**
   * Calling method which does not return any value:
 * calling privateMethod().While executing below line control goes 
   * from here to privateMethod() and executes all statements in 
   * body of privateMethod and after finishing all statements again 
   * control comes to here and execute next statement like   
   * example.getName().
 */
example.privateMethod();

/**
   * getName() is static method. So we are calling this method using class 
   * name.getName() method which does not return any value:
 * calling getName() method
 */
MethodExample.getName();

/**
   * Calling method which returns a float value:
 * When calling a method, you need to provide arguments, which 
   * must be given in the same order as their respective parameters 
   * in the method specification.
 */
float ts = example.getSalary(10000F, 20000F);
System.out.println("Total salary is " + ts);
  }
}

***********************************************************

No comments:

Post a Comment