Tuesday, March 24, 2015

Static class,method,block and variable

Static Class

A Class can be made static only if it is a nested Class. The nested static class can be accessed without having an object of outer class.
Example 1:
class Example1{
  //Static class
  static class X{
      static String str="Inside Class X";
  }
  public static void main(String args[])
  {
      X.str="Inside Class Example1";
      System.out.println("String stored in str is- "+ X.str);
  }
}
Output:
String stored in str is- Inside Class Example1

Static Block

Static block is mostly used for changing the default values of static variables.This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
Example 1: Single static block
class Example3{
   static int num;
   static String mystr;
   static{
      num = 97;
      mystr = "Static keyword in Java";
   }
   public static void main(String args[])
   {
      System.out.println("Value of num="+num);
      System.out.println("Value of mystr="+mystr);
   }
}
Output:
Value of num=97
Value of mystr=Static Keyword in Java

Static Methods

Static Methods can access class variables without using object of the class. It can access non-static methods and non-static variables by using objects. Static methods can be accessed directly in static and non-static methods.
Example 1: public static void main itself is a static method
class Example5{
   static int i;
   static String s;
   public static void main(String args[]) //Its a Static Method
   {
       Example5 obj=new Example5();
       //Non Static variables accessed using object obj
       System.out.println("i:"+obj.i);
       System.out.println("s:"+obj.s);
   }
}
Output:
i:0
s:null

4. Static Variables

  • Static variables are also known as Class Variables.
  • Such variables get default values based on the data type.
  • Data stored in static variables is common for all the objects( or instances ) of that Class.
  • Memory allocation for such variables only happens once when the class is loaded in the memory.
  • These variables can be accessed in any other class using class name.
  • Unlike non-static variables, such variables can be accessed directly in static and non-static methods.
Example 1: Static variables can be accessed without reference in Static method
class Example7{
  static int var1;
  static String var2;
  //Its a Static Method
  public static void main(String args[]) 
  {
      System.out.println("Var1 is:"+Var1);
      System.out.println("Var2 is:"+Var2);
  }
}
Output:
Var1 is:0
Var2 is:null
As you can see in the above example that both the variables are accessed in void main method without any object(reference).
Example 2: Static variables are common for all instances
package beginnersbook.com;
class Example8{
   static int Var1=77; //Static integer variable
   String Var2;//non-static string variable

   public static void main(String args[])
   {
      Example8 ob1 = new Example8();
      Example8 ob2 = new Example8();
      ob1.Var1=88;
      ob1.Var2="I'm Object1";
      ob2.Var2="I'm Object2";
      System.out.println("ob1 integer:"+ob1.Var1);
      System.out.println("ob1 String:"+ob1.Var2);
      System.out.println("ob2 integer:"+ob2.Var1);
      System.out.println("ob2 STring:"+ob2.Var2);
   }
}
Output:
ob1 integer:88ob1 String:I'm Object1 ob2 integer:88 ob2 String:I'ob2 integer:88ob2 String:I'm Object2
In above example String variable is non-static and integer variable is Static. So you can see that String variable value is different for both objects but integer variable value is common for both the instances as all the objects share the same copy of a static variable.

No comments:

Post a Comment