Aug 26, 2011

Methods

        A Java method is a set of Java statements which can be included inside a Java class. Java methods are similar to functions or procedures in other programming languages. Every Java program must have one main() method.
 
Syntax:

                     acessmodifier returntype methodName(arguments)
                     {
                     }
 
More generally, method declarations have six components, in order:
          1. Modifiers—such as public, private, and others you will learn about later.
         2. The return type—the data type of the value returned by the method, or void if the method does not return a value.
         3. The method name—the rules for field names apply to method names as well, but the convention is a little different.
         4.The parameter list in parenthesis—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.
          5.  An exception list—to be discussed later.
        6. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

Example:
 
                        /** Return the max between two numbers */
                        public static int max(int num1, int num2) {
                        int result;
                        if (num1 > num2)
                        result = num1;
                        else
                        result = num2;

                        return result;
                     } 

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.
If the method returns a value, a call to the method is usually treated as a value. For example:
                        int larger = max(30, 40);
If the method returns void, a call to the method must be a statement. For example, the method println returns void. The following call is a statement:
 
                       System.out.println("Welcome to Java!");
 
Example:
Following is the example to demonstrate how to define a method and how to call it:
 
                       public class TestMax {
                       /** Main method */
                       public static void main(String[] args) {
                       int i = 5;
                       int j = 2;
                       int k = max(i, j);
                       System.out.println("The maximum between " + i + " and " + j + " is " + k);
                     }
 
                      /** Return the max between two numbers */
                      public static int max(int num1, int num2) {
                      int result;
                      if (num1 > num2)
                      result = num1;
                      else
                      result = num2;

                      return result;
                     }
Output: 

               The maximum between 5 and 2 is 5
 
This program contains the main method and the max method. The main method is just like any other method except that it is invoked by the JVM.
The main method's header is always the same, like the one in this example, with the modifiers public and static, return value type void, method name main, and a parameter of the String[] type. String[] indicates that the parameter is an array of String.
 
Method Overloading
        The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists.
 
Example:
 
                     Public class OverloadingExample
                    {
                      int x=10;
                      int y=20;
                      int z=30;
                        Public void addNumbers (int i,int j)
                        {
                             int result=i+j;
                        }
                        Public void addNumbers(int i,int j, int k)
                        {
                            int result=i+j+k;
                        }
                    }
 
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, addNumbers(int i,int j) and addNumbers(int i,int j,int k) are distinct and unique methods because they require different argument types.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
 
The finalize( ) Method:
          It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.
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.
The finalize( ) method has this general form:
 
                    protected void finalize( )
                    {
                        // finalization code here
                    }
 
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.


0 comments :

Post a Comment