Sunday 23 October 2016

method

Method
Why methods ?
  • Methods will help us to perform the task.
  • Even without method also we can finish the task but which leads to code more larger.
  • With methods we can splits the task in small pieces for better understanding.
A method contains 2 parts.
  1. method header or method prototype
  2. method body
  • With these two we can write the logic to perform the task based on requirement, mainly it contains return type, method name, method parameters and method body
Syntax 
          returntype mehodname(parameter1, parameter2)
          {

                body of the method;

          }

Example 
          void sum(int x, int y)
          {
                 int z = x+y;
          }
Method signature
  • Method signature represents name of the method and number of parameter of the method.
  • For above example method signature is : sum(int, int)
--------------------------------------------------------------------------
Program        :       Program without methods
Program name :      MethodDemo1.java
Output           :        
                               
class MethodDemo1
{      
        public static void main(String args[])
        {
                System.out.println("ZPH school Lingala village");
                System.out.println();
                System.out.println("Sixth standard Subjects 6");  
                System.out.println("Sixth standard Faculties 6");  
                System.out.println("Sixth standard Students 60"); 
                System.out.println();
                System.out.println("Seventh standard Subjects 7");  
                System.out.println("Seventh standard Faculties 7");  
                System.out.println("Seventh standard Students 70"); 
                System.out.println(); 
                System.out.println("Eighth standard Subjects 8");  
                System.out.println("Eighth standard Faculties 8");  
                System.out.println("Eighth standard Students 80");  
        }
}

Compile          :      javac MethodDemo1.java
Run                :      java MethodDemo1
Output            :        
                             ZPH school Lingala village

                             Sixth standard Subjects 6
                             Sixth standard Faculties 6
                             Sixth standard Students 60

                             Seventh standard Subjects 7
                             Seventh standard Faculties 7
                             Seventh standard Students 70

                             Eighth standard Subjects 8
                             Eighth standard Faculties 8
                             Eighth standard Students 80
--------------------------------------------------------------------------
Program        :       Program with methods
Program name :      MethodDemo2.java
Output            :        
                               
class MethodDemo2
{      
        public void school()
        {
                System.out.println("ZPH school Lingala village");
                System.out.println();
        }        
        public void sixthStandard()
        {
                System.out.println("Sixth standard Subjects 6");  
                System.out.println("Sixth standard Faculties 6");  
                System.out.println("Sixth standard Students 60"); 
                System.out.println();
        }        
        public void seventhStandard()
        {
                System.out.println("Seventh standard Subjects 7");  
                System.out.println("Seventh standard Faculties 7");  
                System.out.println("Seventh standard Students 70"); 
                System.out.println(); 
        }
        public void eighthStandard()
        {
                System.out.println("Eighth standard Subjects 8");  
                System.out.println("Eighth standard Faculties 8");  
                System.out.println("Eighth standard Students 80");  
        }
        public static void main(String args[])
        {
                MethodDemo2 m = new MethodDemo2();
                m.school();
                m.sixthStandard();
                m.seventhStandard();
                m.eighthStandard();
        }
}

Compile          :      javac MethodDemo2.java
Run                :      java MethodDemo2
Output            :        
                             ZPH school Lingala village

                             Sixth standard Subjects 6
                             Sixth standard Faculties 6
                             Sixth standard Students 60

                             Seventh standard Subjects 7
                             Seventh standard Faculties 7
                             Seventh standard Students 70

                             Eighth standard Subjects 8
                             Eighth standard Faculties 8
                             Eighth standard Students 80
--------------------------------------------------------------------------
Types of methods
  1. Instance methods
  2. static methods
1. Instance methods
  • Instance methods will use instance variables to perform the task.
  • We will declare instance methods without static keyword.
  • We will call instance methods by using object name.
--------------------------------------------------------------------------
Program        :       Program on instance methods
Program name :      MethodDemo3.java
Output           :        
                            Sixth standard Subjects 6
                            Sixth standard Faculties 6
                            Sixth standard Students 60
class MethodDemo3
{      
        public void sixthStandard()
        {
                System.out.println("Sixth standard Subjects 6");  
                System.out.println("Sixth standard Faculties 6");  
                System.out.println("Sixth standard Students 60"); 
        }        
       
        public static void main(String args[])
        {
                MethodDemo3 m = new MethodDemo3()
                m.sixthStandard();
        }
}

Compile          :      javac MethodDemo3.java
Run                :      java MethodDemo3
Output            :        
                            Sixth standard Subjects 6
                            Sixth standard Faculties 6
                            Sixth standard Students 60
--------------------------------------------------------------------------
1. Static methods

  • We will declare static methods with static keyword.
  • We will call static methods by using class name or object name.
--------------------------------------------------------------------------
Program        :       Program on static methods
Program name :      MethodDemo4.java
Output           :        
                            ZPH school Lingala village
                             
class MethodDemo4
{      
        public static void school()
        {
                System.out.println("ZPH school Lingala village");
        }        
       
        public static void main(String args[])
        {
                MethodDemo4.school();
        }
}

Compile          :      javac MethodDemo4.java
Run                :      java MethodDemo3
Output            :        
                            ZPH school Lingala village
--------------------------------------------------------------------------
Note
  • Instance variable are not available to static method.
  • Static variables are directly available to static method.
Note
  • Instance variables are the variables whose separate copy is created in every object
  • Static variables are the variables whose single copy in memory is shared by all objects
Thanks for your time.
Nireekshan