Saturday, December 11, 2010

Static variable and method


Static Keyword

  • The static variables are created only once for all objects.
  • The value of static variables is the last modified value.
  • The static method only access the static variable.
  • The static keyword u can’t apply for constructor.
  • The static block is first executed. The static variable and method u can access from non-static method and constructor.
  • Without creating object we can access the Static members and methods.
  • It won’t support the this and super keyword inside the static method.
  • We can apply static keyword only inner class

Example of Static
public class Sta {
static int a=100; int b=200;
static void show(){
System.out.println(“non_static var=“+b); è // we can’t access non static variable
System.out.println("static show method"); inside of static method….
}
Static{ è // static block
System.out.println("static block");
}
static int disp(int b){
return b;
}
public static void main(String[] args){
System.out.println("static variable=="+Sta.a);
Sta.show();
System.out.println(Sta.disp(300));
}
}



No comments:

Post a Comment