Thursday, March 17, 2011

JAVA Programming : Methods

JAVA Methods



A Java method contains set of Java statements which is declared inside a Java class. Methods are where class logic is stored or where the actual work gets done. Java methods are similar to functions or procedures in other programming languages.

A Java method declaration various parts like Modifiers, Return Type, Method name, parameter and and there is one more thing exceptions that can also be part of a method.

Syntex of a Java Method :

    Modifier ReturnType MethodName ( Parameters ){  
              // set of java statements                                    
    }


Example of a Java Method :


  public int giveSum (int a, int b){       

    int ans = a + b;
    return ans;  

  }



Once a method is defined, it can be called with in a Java class just by writing name of the method followed by parameters in the brackets according to method definition and followed by a semicolon. For example if above method giveSum is to called then :


   giveSum(10,20);  //calling a method     


No comments:

Post a Comment