Wednesday, September 15, 2010

static keyword in java...

  •  When a variable is declared with the static keyword, its called a class variable. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create an instance.
  • Without the “static” keyword, it's called instance variable, and each instance of the class has its own copy of the variable.
  • Static block are execute even before the constructors are execute.
  • When the class is first loaded static blocks can be called once.
  • Static members can be execute without object creation.The following example
    illustrates about the static variables, static methods and static blocks.

Ex:-

 public class Test
{  
    public static void main(String[] args)
    {
        StaticDemo sd = new StaticDemo();
        StaticDemo.staticMethod();
    }
}

class StaticDemo
{
    public static final int x=10;
    public static final int y;
    static
    {
        y=20;
        System.out.println("X value is = "+x+" \nY value is = "+y);
    }
    public static void staticMethod()
    {
        System.out.println("Inside the static method of StaticDemo class");
    }
    StaticDemo()
    {
        System.out.println("Inside the constructor of staticDemo class");
    }
}


Output::

X value is = 10
Y value is = 20
Inside the constructor of staticDemo class
Inside the static method of StaticDemo class

          In the above example demonstrates that the order of execution is.

          >>  static variables initialize first then static block will be executed and then static
                 method will be  executed.
          >> Here constructor is called finally . This reveals that all static content can be
                execute before object  creation.

          >> The static method can access only static data
          >> It is illegal to refer a instance variable inside the static method or static block, that
                means it will be compilation error.
          >> static methods and static variables can be called  y using class name with "."
                operator like

                 Test.x;
                 Test.staticMethod();

                 Here "Test" is the class which contains the static variable "x" and static method
                 "staticMethod()"
  • Methods can also be declared with the static keyword. When a method is declared static, it can be called/used without having to create a object first. For example, you can define a collection of math functions in a class, all static, using them like functions
  • Static methods can only call the other static methods
  • Methods declared with “static” keyword are called “class methods”. Otherwise they are “instance methods”.
  • There's no such thing as static classes. “static” in front of class creates compilation error, but inner classes can be declared as static 

Static Inner Classes
  • A class itself is not a static class . But we can declare a nested class as static. Here the static keyword    in the class says that the nested class is a static member of the outer class which contains the inner class  . For example

Ex:-

      class Outer
{
    static class InnerCls {
        void hi()
        {
            System.out.println("Hi");
        }
    }
}

public class InnerDemo
{
    static class InnerDC {
        void dcHi()
        {
            System.out.println("DC HI");
        }
    }
    public static void main(String[] args)
    {
        Outer.InnerCls inner = new Outer.InnerCls();
        inner.hi();
        InnerDC innerdc = new InnerDC();
        innerdc.dcHi();
    }
}

 Output::

    Hi
    DC HI

    No comments:

    Post a Comment