Friday, 3 January 2014

Static Variable and this keyword

Static is a keyword in java. Static can be used in three scenarios:
1.     Static variables
2.     Static methods
3.     Static blocks of code
Static Variable(Class Variable):  

  • It is a variable which belongs to the class and not to object(instance).
  • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
  • single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object
  • Syntax : <class-name>.<variable-name>
Static method:
  • It is a method which belongs to the class and not to the object(instance) 
  • A static method can access only static data. It can not access non-static data (instance variables) 
  • A static method can call only other static methods and can not call a non-static method from it.
  • A static method can be accessed directly by the class name and doesn’t need any object .
  • Syntax : <class-name>.<method-name>
  • A static method cannot refer to “this” or “super” keywords in anyway.
  • A static method can have non static data.
Static block:
  • static block helps to initialize the static data members, just like constructors help to initialize instance members.
  • The static block, is a block of statement inside a Java class that will be executed when a class is first loaded in to the JVM.

    this keyword: 
    • “this” is a reference to the current object, whose method is being called upon.
    • You can use “this” keyword to avoid naming conflicts in the method/constructor of your instance/object.
    • It is a hidden reference.
    Example: In this program even though swap function is called the numbers will not be swapped.
     It is because we are using the same name as that of global variables and local variables.
      


    public class Swap {

    /**
    * This is a program for swapping of two integer numbers. 
    * @Author Snehal
    */
    int num1=10 ;
    int num2=20;

    public void swap(int num1, int num2)
    {
    int temp=0;
    temp = num1;
    num1 = num2;
    num2 = temp;
    }
    public static void main(String[] args) {
    Swap obj = new Swap();
    System.out.println(obj.num1+"  "+obj.num2);
    obj.swap(10,20);
    System.out.println(obj.num1+"  "+obj.num2);
    }

    }

    Output: 10 20
                 10 20





     If  here we have had used other names than num1, num2 in swap function then numbers will be swapped.

    public class Swap {

    /**
    * This is a program for swapping of two integer numbers. This program provides the use of this keyword.
    * @Author Snehal
    */
    int num1=10 ;
    int num2=20;

    public void swap(int num, int numbr) 
    {
    System.out.println(num+"  "+numbr);// numbers passed are 10 20
    int temp=0;
    temp = num;
    num = numbr;
    numbr = temp;
    System.out.println(num+"  "+numbr);// after swapping prints 20 10
    }
    public static void main(String[] args) {
    Swap obj = new Swap();
    //System.out.println(obj.num1+"  "+obj.num2);
    obj.swap(10,20);
    }

    }
    Output: 10 20
                20 10















    Example of
    this keyword: 
    public class Swap {

    /**
    * This is a program for swapping of two integer numbers. This program provides the use of this keyword.
    * @Author Snehal
    */
    int num1=10 ;
    int num2=20;

    public void swap(int num1, int num2)
    {
    int temp=0;
    temp = num1;
    this.num1 = num2;
    this.num2 = temp;
    }
    public static void main(String[] args) {
    Swap obj = new Swap();
    System.out.println(obj.num1+"  "+obj.num2);
    obj.swap(10,20);
    System.out.println(obj.num1+"  "+obj.num2);
    }

    }

    Output: 10 20
                 20 10


    1 comment: