Wednesday 23 October 2013

Session 5:-Role of Methods in java.

         Method

  • Method is nothing but a function it has functionality.
  • Functionality like printing message or it called be difficult functionality like user authentication.
    •  Ex: " hello java" ,User Logic:( 12+34,43-21...etc).
  • Method can accept parameter/argument we called as input parameter (I/P).
  • If any method returning some value than data type of method is the returning values data type.
  •  If method does not return any value than data type is void.
  • return statement is last executable statement.
  • Variable inside the method is called as the local variable. Local variable must be initializing before the use and it scope is within a method.
NOTE:-Method name must be start with small later (Industry Standard).
 
  • Advantage:-
    • Methods are reusable.
    • We can pass dynamic value at the  calling method.
    • We have individual choice of calling method based on the requirement.
    • We can’t define one method inside the other method.
     Syntax:-

                 Access Specifier /modifier   data-type method-Name (parameter/argument)
                    {
                        // coding
                    }
         
          Example :-

         Public void JNext() 
          { 
            Public void training (){ 
         // Compilation error, we can’t define one method inside the other method. 
           } 
          Public void development (); 
          // we can call another method but we can’t define one method inside the other method...!! 
          }
         
         Example :-
        
           public class Run{
           public static void main(String args[]){
           Math m=new Math();
            m.addNum(); //  Invoke addNum()
          } } 
           public class Math{
          void addNum()
         {
         int a=20; // local variable a
         int b=25; // local variable b
         int c=a+b; // logical statement
        System.out.println(c); // printing statement.
        int d=a-b;
        int f=a*b;
        System.out.println(d);
        System.out.println(f);
         } }

        Output:45,-5,500


1 comment: